binascii.b2a vs ord()

Chris Angelico rosuav at gmail.com
Sun Jan 10 15:24:46 EST 2021


On Mon, Jan 11, 2021 at 7:09 AM Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>
> On Sun, 10 Jan 2021 03:29:37 -0000 (UTC), Bischoop <Bischoop at vimart.net>
> declaimed the following:
>
> >I wanted to learn about conversion string to Ascii.
> >So I learn about binascii.b2a but because the output wasn't what I
> >wanted got deeper and found out about ord(c) and actually that's what
> >I'expected.
> >So what's that binascii and why I cant convert ascii that I got from ord
> >to string by using char, instead I'm getting some strings in weird
> >coding.
> >
> >
> >import binascii
> >Ttext = b'This is a string'
> >text2 = 'This is a string'
>
>         Item: this is a Unicode string. Python Unicode strings are only 1-byte
> per character IF all characters are in the 7-bit ASCII range. If you have
> any extended characters (which would, say, be one byte in ISO-Latin-1) they
> could turn the entire Unicode string into 2-byte per character (and really
> expanded sets could be 3 or 4 bytes per character).
>

Irrelevant - they're not "one byte per character" except in terms of
memory usage, which isn't coming up here. When you call ord() on a
character, you get the ordinal of that character, and the internal
representation doesn't matter.

ChrisA


More information about the Python-list mailing list