I am struggeling to convert a byte array (bytes) back to a singed integer. I found a lot of examples which are valid for having positive integers after the conversion but no working example how to handle a negative number.
#example for positive numbers is working fine
data = I2C.read_i2c_block_data(I2CAddress, I2CRegister, 4)
result = 0
for b in data:
result = result * 256 + int(b)
I am running python on a raspberry with python version 2.7.9.
It would be nice to get a basic algorithm for that.
My goal is to convert the byte array from variable data back to a negative number.
variable data has these items in its array:
[0xff, 0xff, 0xff, 0xe7]
Target negative number is -25
(which is obviously 0xe7
)
Cheers