Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Extract double in binary file
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Christopher A. Craig  
View profile  
 More options Nov 26 2003, 1:01 pm
Newsgroups: comp.lang.python
From: list-pyt...@ccraig.org (Christopher A. Craig)
Date: 26 Nov 2003 12:20:01 -0500
Local: Wed, Nov 26 2003 12:20 pm
Subject: Re: Extract double in binary file

pascal.par...@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-pyt...@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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.