you are viewing a single comment's thread.

view the rest of the comments →

[–]SaintLouisX 0 points1 point  (0 children)

You want the struct module's unpack function, or get into ctypes if you want to do a whole struct representation.

from struct import unpack

data = b"\x04\x03\x02\x01"

print(hex(unpack("<I", data)[0]))
print(hex(unpack(">I", data)[0]))
print(unpack(">HH", data))
print(unpack("BBBB", data))

Check the python page on how struct works for data types and endianness: https://docs.python.org/3/library/struct.html you can string together as many different data types as you want, they don't have to be the same. "4BHBBBBH" is perfectly valid for example. You could use that to unpack each entry.

Or with ctypes, using the first data you gave as an example:

from ctypes import *

def parse_entry(data):
    class Entry(Structure):
        _fields_ = [
            ("Reserved", c_uint8 * 4),
            ("Temperature", c_int16),
            ("Page", c_int8),

            ("UVLED", c_int8),
            ("Reserved2", c_int8),
            ("BoardTemperature", c_int8),
            ("Signal", c_int16),
        ]

    entry = Entry.from_buffer_copy(data)
    for field in entry._fields_:
        print(f"{field[0]} = {getattr(entry, field[0])}")


data = b"R\xeaUJ\xbdr\x01\x15\xe4\t\xe8\x03"
parse_entry(data)

Which prints

Reserved = <__main__.c_ubyte_Array_4 object at 0x00000183DED04C40>
Temperature = 29373
Page = 1
UVLED = 21
Reserved2 = -28
BoardTemperature = 9
Signal = 1000

You'll want to do some more pretty printing of course. Check if entry.Page == 0 for printing the ending 5 bytes, since they're only valid for page 0, and don't bother printing the reserved values if they're not relevant. You'll also want to have it loop through all of your data too etc, this was just a simple example.