Python double precision floating point hexadecimal reading

I am trying to execute an unpack hex string in double in Python.

When I try to unpack the following:

 unpack('d', "4081637ef7d0424a"); 

I get the following error:

 struct.error: unpack requires a string argument of length 8 

This doesn't really matter to me, because double is 8 bytes long and

2 characters = 1 hexadecimal value = 1 byte

Thus, in essence, a double length of 8 bytes will be a hexadecimal string of 16 characters.

Any pointers to unpacking this hexadecimal code to a double would be greatly appreciated.

+5
source share
2 answers

First you need to convert the hexadecimal digits to a binary string:

 struct.unpack('d', "4081637ef7d0424a".decode("hex")) 

or

 struct.unpack('d', binascii.unhexlify("4081637ef7d0424a")) 

The latest version works both in Python 2 and 3, the first only in Python 2

+5
source

Try the following:

 a = "\x40\x81\x63\x7e\xf7\xd0\x42\x4a" unpack('d', a); 
0
source

All Articles