Reputation: 3707
I am retrieving bytes of data from an IC2 device using the read_i2c_block_data function. Within this block of data are signed 16-bit values that I want to display. I am able to print out the value correctly using the following:
print ("PC12 : %d mW" % int.from_bytes((return_data[5],return_data[6]), byteorder='little', signed=True))
Though, I have been trying print out the value using the following, which doesn't work and I don't know why. I am curious to why I can't get it to work.
print ("PC12 : %d mW" % int.from_bytes(return_data[5:6], byteorder='little', signed=True))
Does anyone know what I am doing wrong? I thought I could specify the range in the from_bytes function.
Thanks, Mark
Upvotes: 1
Views: 692
Reputation: 55524
return_data[5:6]
returns an array consisting of a single element at index 5:
>>> return_data = b'\00\01\02\03\04\05\06'
>>> return_data[5:6]
b'\x05'
Since you want to convert a 16-bit integer, you need to use return_data[5:7]
.
Upvotes: 2