user188956
user188956

Reputation:

Convert from hex string to unicode

How can i convert the 'dead' string to an unicode string u'\xde\xad'?

Doing this:

from binascii import unhexlify
out = ''.join(x for x in [unhexlify('de'), unhexlify('ad')])

creates a <type 'str'> string '\xde\xad'

Trying to use the Unicode.join() like this:

from binascii import unhexlify
out = ''.join(x for x in [u'', unhexlify('de'), unhexlify('ad')])

results in an error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xde in position 0: ordinal not in range(128)

Upvotes: 2

Views: 7453

Answers (2)

Luk&#225;š Lalinsk&#253;
Luk&#225;š Lalinsk&#253;

Reputation: 41326

Unicode is designed to be compatible with Latin-1, you can use that and simply decode the bytestring:

In [2]: unhexlify('dead').decode('latin1')
Out[2]: u'\xde\xad'

Upvotes: 5

csl
csl

Reputation: 11368

See this Python unicode how-to, and use something akin to:

unicode('\x80abc', errors='replace')

or

unicode('\x80abc', errors='ignore')

Upvotes: 1

Related Questions