Question of UTF16BE encoding / decoding

Mark Tolonen metolone+gmane at gmail.com
Tue May 5 07:21:29 CEST 2009


"Napalmski" <ur.liam at iksmlapan.reverse> wrote in message 
news:MPG.2469d7edf8bbcd0a989683 at eu.news.astraweb.com...
> Hello,
>
> I have an encoded string in the form "004e006100700061006c006d", if you
> split on every 4 characters it decodes to a single character.
> I have come up with this:
>
> name = '004e006100700061006c006d'
> name2 = ""
> for x in range(0, len(name), 4):
>    name2 = name2 + chr(int(name[x:x+4], 16))
>
> Is there a better way to do this using name.decode() that would then
> also work with .encode()?
>
> I have tried .decode("utf-16-be"), which I beleive is the format here,
> but I am getting the error:
> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-
> 11: ordinal not in range(128)
>
> And my debugger seems to show a load of unicode when it breaks.

import binascii
s = '004e006100700061006c006d'
h = binascii.unhexlify(s)
print h.decode('utf-16-be')

-Mark





More information about the Python-list mailing list