I am trying to talk to a device using python. I have been handed a tuple of bytes which contains the storage information. How can I convert the data into the correct values:
response = (0, 0, 117, 143, 6)
The first 4 values are a 32-bit int telling me how many bytes have been used and the last value is the percentage used.
I can access the tuple as response[0] but cannot see how I can get the first 4 values into the int I require.
See Convert Bytes to Floating Point Numbers in Python
You probably want to use the struct module, e.g.
Assuming an unsigned int. There may be a better way to do the conversion to unpack, a list comprehension with join was just the first thing that I came up with.
EDIT: See also ΤΖΩΤΖΙΟΥ’s comment on this answer regarding endianness as well.
EDIT #2: If you don’t mind using the array module as well, here is an alternate method that obviates the need for a list comprehension. Thanks to @JimB for pointing out that unpack can operate on arrays as well.