Python Forum
hex decoding in Python 3 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: hex decoding in Python 3 (/thread-18267.html)



hex decoding in Python 3 - rdirksen - May-11-2019

I am converting a program from P2.7 to P3.5
I receive from a serial interface an array of bytes.
When I print the array in P2.7 I get this:
Raw     ['07', 'f3', '07', 'f0', '00', 'd2', '09', '50', '47', '53', '53', '49', '0f', '28', '28', '28', '95', '07', '0f']
If I print the same result in P3.5 I get this:
Raw     [b'\x07', b'\xf3', b'\x07', b'\xf0', b'\x00', b'\xd2', b'\t', b'P', b'H', b'S', b'S', b'J', b'\x0f', b'(', b'(', b'(', b'\x97', b'\x07', b'\x0f', b'']
basically the same data but another representation. look e.g. to Raw[7] it is 0x50 but in P3.5 b'P'. Basically the same value (Decimal 80) but represented in hex or ascii.

I need to do calculations whith the data so I want to convert de hex value in an integer by
test = int(raw[7], 16)
and I get the error
Error:
ValueError: invalid literal found for int() with base 16: b'P'
How do I preserve the hex data in the Raw array of bytes?

However it seems that the problem is occuring earlier in the process:
I use the Serial library with the following code:
        response = []
        serialConnection.write(command)
#       Wait for response to be acquired or the connection to time out

        while response[-2:] != [b'\x07', b'\x0f'] and time.time() - start < self.timeout:
            response.append (serialConnection.read())
it looks to me that the response.append command interprets the bytes read from the interface and converts them into strings like b'P' for 0x50


RE: hex decoding in Python 3 - heiner55 - May-12-2019

#!/usr/bin/python3
raw =  [b'\x07', b'\xf3', b'\x07', b'\xf0', b'\x00', b'\xd2', b'\t', b'P', b'H', 
        b'S', b'S', b'J', b'\x0f', b'(', b'(', b'(', b'\x97', b'\x07', b'\x0f', b'']
print(raw)

# 16bit big endian
num = ord(raw[7]) * 256 + ord(raw[8])
print(num)

# 16bit little endian
num = ord(raw[8]) * 256 + ord(raw[7])
print(num)

Maybe it is easier if you work with byte-strings instead of array of bytes:

        
response = b''
...
while response[-2:] != b'\x07\x0f' and time.time() - start < self.timeout:
    response += serialConnection.read()
#!/usr/bin/python3
raw = b'\x07\xf3\x07\xf0\x00\xd2\tPHSSJ\x0f(((\x97\x07\x0f'
print(raw)

# big endian
num = int(raw[7]) * 256 + int(raw[8])
print(num)

# little endian
num = int(raw[8]) * 256 + int(raw[7])
print(num)



RE: hex decoding in Python 3 - rdirksen - May-12-2019

Quote:Maybe it is easier if you work with byte-strings instead of array of bytes:

        
response = b''
...
while response[-2:] != b'\x07\x0f' and time.time() - start < self.timeout:
    response += serialConnection.read()

thanks. I opted for your 2nd suggestion.
The 1st and 3rd suggestion apply to two byte words and/or the conversion from utf-8 to numerical. Would not work in my case.

the second option works like a charme.

thanks very much