I'm trying to decode a bytes object, and I'm getting an error, even though by object is a bytes class of 4 bytes.
what I'm calling:
struct.unpack('>f', reg_hex[2:10])[0]
If I print the value of reg_hex[2:10]:
print("reg_hex: {}".format(reg_hex[2:10]))
reg_hex: b'3a8a1b45'
If I print the type of reg_hex[2:10]:
print("reg_hex: {}".format(type(reg_hex[2:10])))
reg_hex: <class 'bytes'>
So reg_hex is a bytes class of 4 bytes, however struct.unpack doesn't seem to be happy with it.
Am I doing something wrong?
Those are 8 bytes, not 4. They're the characters 3
, a
, ... etc.
You need first to convert from hex to actual bytes. For example:
bindata = bytes(int(reg_hex[i:i+2], 16) for i in range(0, reg_hex(x), 2))