Anyone recognize this numeric storage format - similar to "float", but not quite

geskerrett at hotmail.com geskerrett at hotmail.com
Fri Aug 26 01:22:23 CEST 2005


I am not sure if you are still watching this thread, but I seem to have
a bit of a problem with the code sample you so graciously provided.
It seems to work in all instances, except the original example I
provided (namely, 1234567890). On my system, the number 1234567890,
gets converted to 1234567895.5.

I made a few changes to your original program, but it is largely the
same with different test samples samples.  Any thoughts ??

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'),
		(999999.99, '703D0AD7FF237494'),
		( 88888.88, '400ad7a3709c2d91'),
		( 22222.22, '400ad7a3709c2d8f'),
		( 33333.33, 'b047e17a54350290'),
		(  1500.34, '7814ae47e18a3b8b'),
		( 42345.00, '0000000000692590'),
		]

def msd2float(bytes):
	if sum(bytes) in [0,72,127]:       #take out values that don't make
sense possible the NaN and Infinity ??
		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*8))
	return (float(acc)*2.0**exp)*((1.,-1.)[sign!=0])

for line in data:
	val = line[0]
	binval = bn.unhexlify(line[1])
	le_bytes = list(st.unpack('BBBBBBBB',binval))
	test = msd2float(le_bytes)
	print " In:",val, "\nOut:",test,"\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




More information about the Python-list mailing list