chr(i) ASCII under Python 3

D

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?

Dorian
 
A

Alf P. Steinbach

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
 
D

Dodo

Le 26/04/2010 22:26, Alf P. Steinbach a écrit :
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 *
 
A

Alf P. Steinbach

Le 26/04/2010 22:26, Alf P. Steinbach a écrit :

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
 
D

Dave Angel

Dodo said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,144
Latest member
KetoBaseReviews
Top