Extract double in binary file

Colin Brown cbrown at metservice.com
Wed Nov 26 20:38:24 CET 2003


"Pascal" <pascal.parent at free.fr> wrote in message
news:e567c03a.0311260616.205efca0 at posting.google.com...
> Hello,
>
> 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?
>
> Thanks

If the number is saved in a floating point representation (IEEE?),
typically [sign][exponent][fraction] then you really need to know
what the type is. For example, I had to make cross-platform real
numbers at one stage and fabricated them as below.

Colin Brown
PyNZ


import math

def vmsR4(real):
 '''vmsR4(real): returns an integer that is equivalent to a VMS real*4 '''
 (m, e) = math.frexp(real)
 if m == 0.0:
  return 0
 else:
  sign = m < 0
  exp = e + 128
  mant = int((16777216L * abs(m)) + 0.5) - 8388608
  return (sign << 15) + (exp << 7) + (mant >> 16) + (mant << 16)







More information about the Python-list mailing list