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


Messages In This Thread
hex decoding in Python 3 - by rdirksen - May-11-2019, 12:16 PM
RE: hex decoding in Python 3 - by heiner55 - May-12-2019, 08:41 AM
RE: hex decoding in Python 3 - by rdirksen - May-12-2019, 11:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Decoding lat/long in file name johnmcd 4 350 Mar-22-2024, 11:51 AM
Last Post: johnmcd
  Enigma Decoding Problem krisarmstrong 4 722 Dec-14-2023, 10:42 AM
Last Post: Larz60+
  json decoding error deneme2 10 3,625 Mar-22-2023, 10:44 PM
Last Post: deanhystad
  flask app decoding problem mesbah 0 2,347 Aug-01-2021, 08:32 PM
Last Post: mesbah
  Decoding a serial stream AKGentile1963 7 8,524 Mar-20-2021, 08:07 PM
Last Post: deanhystad
  xml decoding failure(bs4) roughstroke 1 2,254 May-09-2020, 04:37 PM
Last Post: snippsat
  python3 decoding problem but python2 OK mesbah 0 1,802 Nov-30-2019, 04:42 PM
Last Post: mesbah
  utf-8 decoding failed every time i try adnanahsan 21 10,828 Aug-27-2019, 04:25 PM
Last Post: adnanahsan
  Decoding log files in binary using an XML file. captainfantastic 1 2,419 Apr-04-2019, 02:24 AM
Last Post: captainfantastic
  decoding sub.process output with multiple \n? searching1 2 2,789 Feb-24-2019, 12:00 AM
Last Post: searching1

Forum Jump:

User Panel Messages

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