Unpacking Binary Data - not using struct module
geskerrett at hotmail.com
Thu Dec 16 02:17:22 CET 2004
I am working on a file conversion project that reads data from a one
file format, reformats in and writes in out to another. The data is
records of informations - names address, account number,statistics.
The numeric values in the original file are stored in what appears to
be a "packed" data format,using a structure that does not use any of
the more standard "C" formats, so I can't use the "struct" module.
As an example, the number "1130" is store using 3 bytes. The HEX
values of the 3 bytes would be 0x01,0x13,0x0F. In other words, the
hex value value "01130f" is unpacked to become "1130".
I believe the original program is written in a version of "Basic" that
dates from the early 90's if that helps.
Currently, I have no problem reading the data. I read the three bytes
and use the binascii.hexilify() function convert the hex values to
their corresponding ASCII values, and then slice off the trailing "f".
In other words;
unpackednumber = int(binascii.hexlify(0x1130f)[:-1])
My questions ...
1) Does anyone recognize this numeric format ?
2) Is there a more efficient module/function I can use, or is the
above the best method?
3) I think that the trailing "f" is sometimes used to indicate the
sign of the number and perhaps even the number of decimal places ...
but am not sure.
Any advice/guidance would be appreciated.
More information about the Python-list