I am decrypting a binary that has decimal numbers represented by four bytes, a little endian. For example, 94 53 F0 40represents 7.510202. Unfortunately, Python gives me 7.51020240784.
When I try to analyze this data with unpack("<f",sampledata)[0], I do not get the exact representations of the original due to the way Python stores the values ββ(for more information see http://bugs.python.org/issue4114 ).
Unfortunately, I need to get the same idea - regardless of the discussion about the inaccuracy of floats, because I need to write these values ββto a text file with the same number of decimal places, since they were originally written to a binary file with.
I would rather stick with Python if possible, but I am happy to implement the solution in C if necessary. The reason why I cannot just trim the return of the decompression function is because I cannot guarantee how many decimal places the original float has, for example it 0C 02 0F 41represents 8.938 according to my hex editor from the source binary, which has only 3 decimal places .
To be clear, I need to take four hexadecimal bytes as my input and output either text / ASCII or a numeric representation of the 32-bit IEEE floating point number, which has the same number of decimal places as the creator of the file provided. I will use the result to create the CSV source file of binary data, and not to perform any calculations.
Any suggestions?
Example:
from __future__ import print_function
from struct import *
print("Should print 7.510202")
hexbytes = b"\x94\x53\xF0\x40"
print(unpack("<f",hexbytes)[0])
Chris source
share