Extract double in binary file
Christopher A. Craig
list-python at ccraig.org
Wed Nov 26 12:20:01 EST 2003
pascal.parent at free.fr (Pascal) writes:
> I've a binary file with data in it.
> This file come from an old ms dos application (multilog ~ 1980).
> In this application, a field is declared as a 'decimal' (999 999
> 999.99).
> I put 0.00 in the field and save the record to the file.
> When I look in the binary file (with python or an hex editor), the
> field is stored on 8 bytes: 00-00-00-00-00-00-7F-00.
> I try unpack from struct module but the result isn't good.
>
> Can someone help me?
You're sort of vague here, but I don't think struct is going to help
you regardless. "decimal" in this case is almost certainly some sort
of BCD, which isn't a standard C struct (and therefore unknown to the
struct module).
You really need to figure out how the data is stored. Based on your
one example it looks like it's stored as a series of 7 bit values
representing the decimal digits with 0x7f indicating the decimal
point. If this is correct you could use something like
tstr=''
for c in instr:
if c == chr(0x7f):
tstr+='.'
else:
tstr += str(ord(c))
fl = float(tstr)
With two major caveats:
1) that this is going to return a float, not a decimal
2) There's no way for me to even guess how negative numbers are
represented
--
Christopher A. Craig <list-python at ccraig.org>
"By rights we shouldn't be here." -- Sam in Peter Jackson's
"The Two Towers" while standing in Osgiliath, where he shouldn't be.
More information about the Python-list
mailing list