I have a bytearray which consist of 4 bytes where each byte represents a singed byte in the range of -128..127. How to convert this?
The byte-array representing the values: -1, 15, 1 and -2 is created by:
data = bytearray()
data.extend([0xff, 0x0f, 0x01, 0xfe])
Now I try to convert it with this code:
import struct
my_signed_byte_0 = struct.unpack("b", data[0])[0]
my_signed_byte_1 = struct.unpack("b", data[1])[0]
my_signed_byte_2 = struct.unpack("b", data[2])[0]
my_signed_byte_3 = struct.unpack("b", data[3])[0]
This raises error:
TypeError: a bytes-like object is required, not 'int'
This happens, because data[0] does return an int and not a bytearray.