[Tutor] Re: How to read unicode strings from a binary file and display them as plain ascii?

Javier Ruere javier at ruere.com.ar
Tue Mar 1 05:51:18 CET 2005


R. Alan Monroe wrote:
> I started writing a program to parse the headers of truetype fonts to
> examine their family info. But I can't manage to print out the strings
> without the zero bytes in between each character (they display as a
> black block labeled 'NUL' in Scite's output pane)
> 
> I tried:
>      stuff = f.read(nlength)
>      stuff = unicode(stuff, 'utf-8')

   If there are embeded 0's in the string, it won't be utf8, it could be 
utf16 or 32.
   Try:
	unicode(stuff, 'utf-16')
or
	stuff.decode('utf-16')

>      print type(stuff), 'stuff', stuff.encode()
> This prints:
> 
>      <type 'unicode'> stuff [NUL]C[NUL]o[NUL]p[NUL]y[NUL]r[NUL]i[NUL]g[NUL]

   I don't understand what you tried to accomplish here.

> Apparently I'm missing something simple, but I don't know what.

   Try the other encodings. It probably is utf-16.

Javier



More information about the Tutor mailing list