Ad

Reading A Binary File Given The Data Structure

I am facing an issue in correctly reading the contents of a binary file, probably due to incorrect unpacking of the data structure (which is why I am here- because I don't know how to unpack a data structure, given different data types).

The documentation did not help me much, just led me to the standard list of data types and their abbreviations and a not-so-helpful illustration.

I am trying to understand the following code:

data = struct.unpack('<hlhcl6s10s11s10s2s10schc', b[i:i+66])

which "should" correspond to the 'last traded price' in this data structure (attached the picture): Data Structure of the binary file

I can understand that the 6s , 10s , etc. mean the character lengths. But what is the significance of the 'hlhcl' in the beginning and 'chc' in the end. The data structure contains a 'long' (which should just be a 'h', according to the documentation) followed by a lot of characters (which should be written as 'c6s10s11s10s2s1s10s12s10s12s'.

If you could help me write the correct sequence of characters to get the 'last traded price', it'd be very helpful. Thanks in advance.

Ad

Answer

If you have the binary data on hand (b, right?) and you are only interested in one field, why can you not just access that field directly?

last_traded_price_bytes = b[88:98]

Might want to double check those numbers, pretty sure it started at the 88th byte.

Ad
source: stackoverflow.com
Ad