Search code examples
pythondoublehex

Unpack from hex to double in Python


Python: Unpack from hex to double

This is the value

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

I tried

unpack('d', value)

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


Solution

  • Use ''.join 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,)