[Python-Dev] unicode/string asymmetries

Fredrik Lundh fredrik@pythonware.com
Wed, 9 Jan 2002 10:02:12 +0100


thomas wrote:

> Hehe, I don't want to put objects in structures, I just want to buid
> structures containing "Unicode strings".

there is no such thing.

what you want is a binary buffer with an *encoded*
unicode string.

to get one, figure out what encoding you need (probably
utf-16-le), convert the string to a byte string using the
encode method, and store that byte string in your struct.

def wu(str):
    # encode unicode string for win32 apis
    return str.encode("utf-16-le")

struct.pack("32s", wu(u"VS_VERSION_INFO"))

>
> struct.pack("32s", str(buffer(u"VS_VERSION_INFO")))

that's evil: you're assuming that Python will always use the
same internal representation for unicode strings.  that's not
the case.

</F>