need help understanding: converting text to binary

Cameron Simpson cs at cskk.id.au
Tue Apr 23 17:22:04 EDT 2019


On 23Apr2019 20:35, Eli the Bearded <*@eli.users.panix.com> wrote:
>In comp.lang.python, Chris Angelico  <rosuav at gmail.com> wrote:
>>> Is there a more python-esque way to convert what should be plain 
>>> ascii
>> What does "plain ASCII" actually mean, though?
>
>ASCII encoded binary data. ASCII is code points that fit in 7-bits
>comprising the characters found on a typical 1970s US oriented
>typewriter plus a few control characters.
[...]
>> but I would strongly recommend just
>> encoding and decoding regardless. Use text internally and bytes at the
>> outside.
>
>That feels entirely wrong. I don't know what b'\x9A' means without
>knowing the character set and character encoding. If the encoding is a
>multibyte one, b'\x9A' doesn't mean anything on its own. That's why I
>want to treat it as binary.

If you don't know the encoding then you don't know you're looking at a 
hex digit. OTOH, if the binary data contain ASCII data then you do know 
the encoding: it is ASCII.

If that is mixed with other data then you need to know where it 
starts/stops in order to pull it out to be decoded. The overall data may 
be a mix, but the bit you're pulling out is encoded text, which you 
could decode.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list