Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unpack from hex to double in Python

Tags:

python

double

hex

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.

like image 486
kame Avatar asked Dec 01 '22 10:12

kame


1 Answers

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,)
like image 104
Mark Byers Avatar answered Dec 05 '22 18:12

Mark Byers