Reputation: 237
I have library function which reads from hardware and returns
value = readregister(registerindex,numbytes)
where value
is a python list.
If numbytes
is 2, then the complete returned number will be returned in
value[0],value[1]
For example it returns
[128,2]
which in hex is
[0x80,0x2]
How do I convert this list value of 2 elements to a hex python number ? So if combine them I should get 0x280 ie 640
Upvotes: 0
Views: 11808
Reputation: 40683
There is no need to convert to an intermediate hex representation. You can just left shift the bits to create the number you want.
def get_number(values):
total = 0
for val in reversed(values):
total = (total << 8) + val
return total
Upvotes: 2
Reputation: 1402
try this:
list=[128,2]
>>> b=[]
>>> for item in list:
... b.append(hex(item))
...
>>> b
>>>[0x80,0x2]
Upvotes: 0