Velocity Reviews > Converting from Microsoft Binary Format floats to Python Float

Converting from Microsoft Binary Format floats to Python Float

geskerrett@hotmail.com
Guest
Posts: n/a

 08-26-2005
In the '80's, Microsoft had a proprietary binary structure to handle
floating point numbers, In a previous thread, Bengt Richter posted
some example code in how to convert these to python floats;

I copied this code and modified it slightly, however, you will notice
that for one of the examples, the conversion isn't exact.

Can anyone advise me on how to modify this code to correct for this
situation ?
I think the problem may be related to the different lengths of the
mantissa. For double precision (8bytes) MBF format had 55 where as
Python floats (IEEE) has only 52 ??

Sample Code Below ----------------------
# Conversion of Microsoft Binary Format numbers to Python Floats

import binascii as bn
import struct as st

data = [(1234567890,'000000AF052C139F*'),
(4069954144,'00000060929672A0'*),
( 33333.33, 'b047e17a54350290'),
( 1500.34, '7814ae47e18a3b8b'),
( 42345.00, '0000000000692590'),
]

def msd2float(bytes):
#take out values that don't make sense possibly the NaN and
Infinity ??
if sum(bytes) in [0,72,127]:
return 0.0
b = bytes[:]
sign = bytes[-2]&0x80
b[-2] |= 0x80 #hidden most sig bit in place of sign
exp = bytes[-1] - 0x80 - 56 #exponent offset
acc = 0L
for i,byte in enumerate(b[:-1]):
acc |=(long(byte)<<(i*)
return (float(acc)*2.0**exp)*((1.,-1.*)[sign!=0])

for line in data:
inval = line[0]
binval = bn.unhexlify(line[1])
le_bytes = list(st.unpack('BBBBBBBB',binv*al))
outval = msd2float(le_bytes)
print " In:",inval, "\nOut:",outval,"\n"

Sample Output ------------------------
C:/Python24/pythonw.exe -u "C:/pytest/dms/Test MBF.pyw"
In: 1234567890
Out: 1234567895.5

In: 4069954144
Out: 4069954144.0

In: 999999.99
Out: 999999.99

In: 88888.88
Out: 88888.88

In: 22222.22
Out: 22222.22

In: 33333.33
Out: 33333.33

In: 1500.34
Out: 1500.34

In: 42345.0
Out: 42345.0
----End Sample Output ----

Bengt Richter
Guest
Posts: n/a

 08-27-2005
On 26 Aug 2005 07:55:26 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

>In the '80's, Microsoft had a proprietary binary structure to handle
>floating point numbers, In a previous thread, Bengt Richter posted
>some example code in how to convert these to python floats;
>
>
>I copied this code and modified it slightly, however, you will notice
>that for one of the examples, the conversion isn't exact.
>

I suspect a typo in the hex

>Can anyone advise me on how to modify this code to correct for this
>situation ?
>I think the problem may be related to the different lengths of the
>mantissa. For double precision (8bytes) MBF format had 55 where as
>Python floats (IEEE) has only 52 ??

I believe that's 53 including the "hidden" msb. UIAM that means you
could lose precision if the LS byte had bits below 'F8' -- i.e.,
if bytes[0]&7 != 0 in the bytes passed to msd2float.

But the bits that make the difference between 1234567890.0 and 1234567895.5
are nowhere near there. Hence my suspicion of a typo.

>
>Sample Code Below ----------------------
># Conversion of Microsoft Binary Format numbers to Python Floats
>
>import binascii as bn
>import struct as st
>
>

Are you sure it's not a 4? ----^

> ( 33333.33, 'b047e17a54350290'),
> ( 1500.34, '7814ae47e18a3b8b'),
> ( 42345.00, '0000000000692590'),
> ]
>
>
>def msd2float(bytes):
> #take out values that don't make sense possibly the NaN and
>Infinity ??
> if sum(bytes) in [0,72,127]:
> return 0.0
> b =3D bytes[:]
> sign =3D bytes[-2]&0x80
> b[-2] |=3D 0x80 #hidden most sig bit in place of sign
> exp =3D bytes[-1] - 0x80 - 56 #exponent offset
> acc =3D 0L
> for i,byte in enumerate(b[:-1]):
> acc |=3D(long(byte)<<(i*)
>
>for line in data:
> inval =3D line[0]
> binval =3D bn.unhexlify(line[1])
> outval =3D msd2float(le_bytes)
> print " In:",inval, "\nOut:",outval,"\n"

The above print will use str conversion for floats, so you are
not seeing a full representation of the two values. (BTW, even the repr
value is not necessarily an accurate representation of the exact
decimal represented by the IEEE double's bits, but it is guaranteed
to be converted back to those bits from the literal string representation.)
E.g. (using my exact decimal hack to show the full value),

>>> from ut.exactdec import ED
>>> print .1

0.1

Which is the same as
>>> print str(.1)

0.1

but repr shows that's not exact

>>> print repr(.1)

0.10000000000000001

but the exact decimal representation for those bits is
>>> print ED(.1, 'all')

ED('0.10000000000000000555111512312578270211815834 04541015625')

>
>Sample Output ------------------------
>C:/Python24/pythonw.exe -u "C:/pytest/dms/Test MBF.pyw"
> In: 1234567890
>Out: 1234567895.5
>

try it with the typo fixed

Regards,
Bengt Richter

geskerrett@hotmail.com
Guest
Posts: n/a

 08-28-2005
Well, thank-you again.
It's a bit embarassing, but you are correct ... It was a typo on in
the sample data.
A bit frustrated with myself as I checked, and double checked, but I
guess became a bit blinded to the problem.

Geoff.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Carsten Fuchs C++ 45 10-08-2009 09:47 AM mathieu C++ 9 09-15-2007 09:10 AM Kosio C Programming 44 09-23-2005 09:49 AM Andy Java 7 05-10-2004 09:26 PM bd C Programming 0 07-07-2003 12:09 AM