Search code examples
pythonasciipython-unicode

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


I have a hex string represented as a list of single characters that contains some text. When I try to decode the hex with:

for counter, i in enumerate(hexadecimal):
    if counter % 2 == 0:
        pass
    else:
        temp_list = hexadecimal[counter:counter + 2]
        hex_string = ''.join(str(x) for x in temp_list)
        bytes_string = bytes.fromhex(hex_string)
        ascii_text += bytes_string.decode('ascii')

It works fine but when it encounters the number ['A', 7] it gives me this error. I assume it's because the character doesn't exist in ASCII, how do I make a readable character from that?

EDIT:

An example input would be [6, 1, 6, 2, 6, 3] which yields abc as output.

A program input is [0, 'F', 3, 4, 3, 0, 2, 'E', 3, 0, 3, 1, 5, 6, 2, 0, 3, 4, 3, 7, 3, 2, 3, 3, 4, 1, 4, 1, 3, 3, 5, 1] which yields 40.01V 4723AA3Q


Solution

  • If you want to be able to represent any byte as an acceptable character, you should use the Latin-1 or ISO-8859-1 encoding (2 names but same charset). Any byte is accepted, even if some are not printable characters. The representation is the unicode character of same value (up to 255 of course) if it exists.

    For you question, '\xa7' will be the unicode character U+00A7 SECTION SIGN §.

    So you just have to change your last line with:

    ascii_text += bytes_string.decode('Latin1')