[Python-Dev] unicode/string asymmetries

Thomas Heller thomas.heller@ion-tof.com
Wed, 9 Jan 2002 15:07:57 +0100


From: "Fredrik Lundh" <fredrik@pythonware.com>
> 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"))

Thanks, works great. And utf-16-le *seems* to be what I want...

Next question ;-), sorry for beeing off-topic for python-dev:

How can I do the equivalent of
  u"some string"
in terms of
  unicode("some string", encoding)

Thanks,

Thomas