"convert" string to bytes without changing data (encoding)

Chris Angelico rosuav at gmail.com
Wed Mar 28 12:18:43 EDT 2012


On Thu, Mar 29, 2012 at 2:36 AM, Ross Ridge <rridge at csclub.uwaterloo.ca> wrote:
> Chris Angelico  <rosuav at gmail.com> wrote:
>>What is a string? It's not a series of bytes.
>
> Of course it is.  Conceptually you're not supposed to think of it that
> way, but a string is stored in memory as a series of bytes.

Note that distinction. I said that a string "is not" a series of
bytes; you say that it "is stored" as bytes.

> What he's asking for many not be very useful or practical, but if that's
> your problem here than then that's what you should be addressing, not
> pretending that it's fundamentally impossible.

That's equivalent to taking a 64-bit integer and trying to treat it as
a 64-bit floating point number. They're all just bits in memory, and
in C it's quite easy to cast a pointer to a different type and
dereference it. But a Python Unicode string might be stored in several
ways; for all you know, it might actually be stored as a sequence of
apples in a refrigerator, just as long as they can be referenced
correctly. There's no logical Python way to turn that into a series of
bytes.

ChrisA



More information about the Python-list mailing list