Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hex decoding in Python 3
#1
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
Reply
#2
#!/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)
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  flask app decoding problem mesbah 0 1,466 Aug-01-2021, 08:32 PM
Last Post: mesbah
  Decoding a serial stream AKGentile1963 7 4,857 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  xml decoding failure(bs4) roughstroke 1 1,502 May-09-2020, 04:37 PM
Last Post: snippsat
  python3 decoding problem but python2 OK mesbah 0 1,335 Nov-30-2019, 04:42 PM
Last Post: mesbah
  utf-8 decoding failed every time i try adnanahsan 21 7,427 Aug-27-2019, 04:25 PM
Last Post: adnanahsan
  Decoding log files in binary using an XML file. captainfantastic 1 1,817 Apr-04-2019, 02:24 AM
Last Post: captainfantastic
  decoding sub.process output with multiple \n? searching1 2 2,120 Feb-24-2019, 12:00 AM
Last Post: searching1
  URL DECODING UnionSystems 5 3,180 Jan-02-2019, 05:28 PM
Last Post: UnionSystems
  Decoding Hex / understanding Code NoWay 4 3,224 Mar-20-2018, 02:48 PM
Last Post: NoWay
  base64 decoding issue or bug rbrahmaa 2 11,727 Apr-25-2017, 11:56 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020