Unpacking Binary Data - not using struct module

Scott David Daniels Scott.Daniels at Acm.Org
Thu Dec 16 15:40:18 CET 2004


Geoffrey wrote:
> 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.

It might be some form of floating point.  If you can encode numbers like
11300, that might help (also throw in some negative numbers and some
small numbers).  I'd like 0 and +/- versions of 2**n for n in range(16)
and +/- versions of 10**n for n in range(1,7).

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list