Unpacking Binary Data - not using struct module

G

Geoffrey

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.
 
L

Laurent Pointal

Geoffrey said:
My questions ...
1) Does anyone recognize this numeric format ?

Seem to be some decimal coded binary, where you only use hexa from 0 to
9, coding corresponding decimal digits.
The trailing hexa F may be used to indicate the end of the number (if
number can have variable length) as it is normally not used as a digit
in this coding.
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.

Maybe... but have you an example of a negative number ?

A+

Laurent.
 
S

Scott David Daniels

Geoffrey said:
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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top