Unzip from hex to double in Python

Python: unzip from hex to double

This value

value = ['\x7f', '\x15', '\xb7', '\xdb', '5', '\x03', '\xc0', '@'] 

I tried

 unpack('d', value) 

but he needs a string to unpack. This is a list now. But when I change it to a string, the length will change from 8 to 58. But the double needs a value of length 8.

+4
source share
2 answers

Use ''.join to convert the list to a string:

 >>> value = ['\x7f', '\x15', '\xb7', '\xdb', '5', '\x03', '\xc0', '@'] >>> ''.join(value) '\x7f\x15\xb7\xdb5\x03\ xc0@ ' >>> from struct import unpack >>> unpack('d', ''.join(value)) (8198.4207676749193,) 
+12
source

Please note that there are two ways to convert this to double, depending on whether the processor is large, but it is better to know which one you need.

 >>> from struct import unpack >>> value = ['\x7f', '\x15', '\xb7', '\xdb', '5', '\x03', '\xc0', '@'] >>> unpack('<d', ''.join(value))[0] 8198.42076767492 >>> unpack('>d', ''.join(value))[0] 1.4893584640656973e+304 

and just for fun - here's how to explicitly decode a double

 >>> value = ['\x7f', '\x15', '\xb7', '\xdb', '5', '\x03', '\xc0', '@'] >>> bytes = map(ord,reversed(value)) >>> sign = (1,-1)[bytes[0]>>7] >>> exp = ((0x7f&bytes[0])<<4) + (bytes[1]>>4) - 1023 >>> mantissa = reduce(lambda x,y: (x<<8) + y, bytes[2:], bytes[1]&0xf) >>> sign*2**exp*(1+mantissa*2**-52) 8198.4207676749193 
+6
source

All Articles