python - Decoding an IEEE double precision float (8 byte) -
i'm decoding amf0 format file. data i'm looking timestamp, encoded array. [hh, mm, ss].
since data amf0, can locate start of data reading in file bytes, converting each byte hex, , looking signal 08 00 00 00 03, array of length 3.
my problem don't know how decode 8-byte integer in each element of array. have data in same, hex-encoded format, e.g.:
08 00 00 00 03 *signals array length 3* 00 01 30 00 00 00 00 00 00 00 00 00 *signals integer* 00 01 31 00 00 00 00 00 00 00 00 00 *signals integer* 00 01 32 00 40 3c 00 00 00 00 00 00 *signals integer* 00 00 09 *signals object end*
this should decoded [0, 0, 28] (if minerva believed).
i've been trying use struct.unpack, examples see 4-byte (little endian) values.
the format specifier looking ">9xd4xd4xd3x"
:
>>> import struct >>> binascii import unhexlify >>> struct.unpack(">9xd4xd4xd3x", unhexlify("080000000300013000000000000000000000013100000000000000000000013200403c000000000000000009")) (0.0, 0.0, 28.0)
broken down:
>
: big endian format5x
: 5 bytes begin-of-array marker + size (ignored)4x
: 4 bytes begin-of-element marker (ignored)d
: 1 big endian ieee-754double
- points 2-3 other 2 elements
3x
: 3 bytes end-of-array marker (ignored)
points 1. , 2. merged 9x
.
as might have noticed, struct
can ignore bytes, not validate. if need more flexibility in input format, use regex matching begin/end array markers in non-greedy mode.
Comments
Post a Comment