chr(i) ASCII under Python 3

Discussion in 'Python' started by Dodo, Apr 26, 2010.

  1. Dodo

    Dodo Guest

    Hi all,
    Under python 2.6, chr() "Return a string of one character whose ASCII
    code is the integer i." (quoted from docs.python.org)
    Under python 3.1, chr() "Return the string of one character whose
    Unicode codepoint is the integer i."

    I want to convert a ASCII code back to a character under python 3, not
    Unicode.

    How can I do that?

    Dorian
     
    Dodo, Apr 26, 2010
    #1
    1. Advertising

  2. On 26.04.2010 22:12, * Dodo:
    > Hi all,
    > Under python 2.6, chr() "Return a string of one character whose ASCII
    > code is the integer i." (quoted from docs.python.org)
    > Under python 3.1, chr() "Return the string of one character whose
    > Unicode codepoint is the integer i."
    >
    > I want to convert a ASCII code back to a character under python 3, not
    > Unicode.
    >
    > How can I do that?


    Just use chr().

    ASCII (7-bit) is a subset of ISO Latin-1 (7-bit), which is a subset of Unicode's
    Basic Multilingual Plane (BMP, original Unicode, 16-bit) which is a subset of
    Unicode (21-bit).


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Apr 26, 2010
    #2
    1. Advertising

  3. Dodo

    Dodo Guest

    Le 26/04/2010 22:26, Alf P. Steinbach a écrit :
    > On 26.04.2010 22:12, * Dodo:
    >> Hi all,
    >> Under python 2.6, chr() "Return a string of one character whose ASCII
    >> code is the integer i." (quoted from docs.python.org)
    >> Under python 3.1, chr() "Return the string of one character whose
    >> Unicode codepoint is the integer i."
    >>
    >> I want to convert a ASCII code back to a character under python 3, not
    >> Unicode.
    >>
    >> How can I do that?

    >
    > Just use chr().
    >
    > ASCII (7-bit) is a subset of ISO Latin-1 (7-bit), which is a subset of
    > Unicode's Basic Multilingual Plane (BMP, original Unicode, 16-bit) which
    > is a subset of Unicode (21-bit).
    >
    >
    > Cheers & hth.,
    >
    > - Alf


    Oh, I see... thanks

    * just realize the problem doesn't come from here *
     
    Dodo, Apr 26, 2010
    #3
  4. On 26.04.2010 22:26, * Dodo:
    > Le 26/04/2010 22:26, Alf P. Steinbach a écrit :
    >> On 26.04.2010 22:12, * Dodo:
    >>> Hi all,
    >>> Under python 2.6, chr() "Return a string of one character whose ASCII
    >>> code is the integer i." (quoted from docs.python.org)
    >>> Under python 3.1, chr() "Return the string of one character whose
    >>> Unicode codepoint is the integer i."
    >>>
    >>> I want to convert a ASCII code back to a character under python 3, not
    >>> Unicode.
    >>>
    >>> How can I do that?

    >>
    >> Just use chr().
    >>
    >> ASCII (7-bit) is a subset of ISO Latin-1 (7-bit), which is a subset of
    >> Unicode's Basic Multilingual Plane (BMP, original Unicode, 16-bit) which
    >> is a subset of Unicode (21-bit).
    >>
    >>
    >> Cheers & hth.,
    >>
    >> - Alf

    >
    > Oh, I see... thanks
    >
    > * just realize the problem doesn't come from here *


    Uhm, I meant to write that ISO Latin-1 is 8-bit. Sorry. Keyboard gremlin.


    Cheers,

    - Alf
     
    Alf P. Steinbach, Apr 26, 2010
    #4
  5. Le Mon, 26 Apr 2010 22:26:28 +0200, Alf P. Steinbach a écrit :
    > On 26.04.2010 22:12, * Dodo:
    >> Hi all,
    >> Under python 2.6, chr() "Return a string of one character whose ASCII
    >> code is the integer i." (quoted from docs.python.org) Under python 3.1,
    >> chr() "Return the string of one character whose Unicode codepoint is
    >> the integer i."
    >>
    >> I want to convert a ASCII code back to a character under python 3, not
    >> Unicode.
    >>
    >> How can I do that?

    >
    > Just use chr().


    Or, if you want a bytes object, just use the bytes constructor:

    >>> bytes([65])

    b'A'

    Regards

    Antoine.
     
    Antoine Pitrou, Apr 27, 2010
    #5
  6. Dodo

    Dave Angel Guest

    Dodo wrote:
    > Hi all,
    > Under python 2.6, chr() "Return a string of one character whose ASCII
    > code is the integer i." (quoted from docs.python.org)
    > Under python 3.1, chr() "Return the string of one character whose
    > Unicode codepoint is the integer i."
    >
    > I want to convert a ASCII code back to a character under python 3, not
    > Unicode.
    >
    > How can I do that?
    >
    > Dorian
    >

    Like a lot of things, it depends on why you're asking what you are.

    Characters are in Unicode on Python 3.x, by definition. That's not a
    problem, it's a feature. Such a character is 16 bits, and if it's an
    ASCII value, the bottom 7 bits exactly match ASCII, and the remaining
    ones are zero.

    However, sometimes you don't really want strings of characters, you want
    an "array of 8 bit values," and you're used to the equivalence that
    earlier versions of Python give you. In those cases, sometimes a string
    (Unicode) works transparently, and sometimes you really want a byte
    array. Simplest example is when you're calling a DLL written in another
    language.

    The types bytes and bytearray are considered sequences of integers (each
    of range 0 to 255), rather than characters. And there are ways to
    convert back and forth between those and real strings.

    DaveA
     
    Dave Angel, Apr 27, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. crjunk
    Replies:
    2
    Views:
    16,841
    crjunk
    Aug 8, 2003
  2. TOXiC
    Replies:
    5
    Views:
    1,259
    TOXiC
    Jan 31, 2007
  3. Stu
    Replies:
    1
    Views:
    127
    Ryan Davis
    Dec 3, 2010
  4. James O'Brien
    Replies:
    3
    Views:
    255
    Ben Morrow
    Mar 5, 2004
  5. Alextophi
    Replies:
    8
    Views:
    515
    Alan J. Flavell
    Dec 30, 2005
Loading...

Share This Page