How to format bytes from strings to hex values

628 Views Asked by At

I am reading in a binary file with big5 encoded characters. When I read a double byte sequence, they would appear as a list of bytes e.g.

>>> bytes = ['0xa6', '0x7b']

If I modify these string bytes:

>>> big5_str = ''
>>> for hexVal in bytes:
>>>     newHexVal = '\\' + hexVal[1:]
>>>     big5_str += newHexVal

so they appear as:

>>> print big5_str
>>> '\xa6\x7b'

but big5_str actually has the escape '\' still in the string:

>>> big5_str
>>> '\\xa6\\x7b'

and if i decode using big5, I only get the same string back (due to the double-backslash):

>>> print byte_string.decode('big5')
>>> '\xa6\x7b'

If I explicitly code the the byte sequence as the hex values:

>>> bytes2 = '\xa6\x7b'
>>> print bytes2.decode('big5')
>>> 州

My question is, how can I read these bytes, format them in '\x**' format them so that they are recognized as bytes, not strings, using a non-escaped backslash?

0

There are 0 best solutions below