How to unpack data to unicode string?

Martin Bless m.bless at gmx.de
Mon Jun 4 08:55:55 EDT 2001


How do I unpack pieces of data into unicode strings?

Here's why:
I'm doing a bit of analyzing TrueType files. (BTW, there's some good
information at www.microsoft.com/typography). There can be chunks of
unicode data inside truetype files which I can extract via slicing:
  rawText = data[start:end]

Now, how do I do the transformation of rawText
'\x00N\x00o\x00r\x00m\x00a\x00l'  ->  u'Normal' ?

I don't have an idea of how to do it 'state of the art', so currently
I'm using this little hack:

def makeUnicodeString( s):
    result = u''
    i = 0
    while i<len(s)-1:
        result = result + unichr( ord(s[i])*256+ord(s[i+1]))
        i += 2
    return result

What about 'struct'?
Couldn't struct have something like
 struct.unpack('6u','\x00N\x00o\x00r\x00m\x00a\x00l')  ->  u'Normal' ?

Martin



More information about the Python-list mailing list