Question of UTF16BE encoding / decoding

N

Napalmski

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.

Thank you!
 
M

Mark Tolonen

Napalmski said:
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
 
N

Napalmski

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

-Mark

And:
name2 = name2.encode("utf-16-be")
print binascii.b2a_hex(name2)

to re-encode,
Thank you, much appreciated!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top