hex string to floating point (newbie)

Peter Hansen peter at engcorp.com
Mon Mar 17 15:13:57 CET 2003

Peter Hansen wrote:
> >>> print struct.unpack('<f', h)[0]
> -428967904.0
> >>> print struct.unpack('>f', h)[0]
> 1.10000002384
> The reason there are two possible results is that you haven't
> specified the byte-ordering of the floating point data.  One
> could guess, perhaps correctly, by its form that it's big-endian,
> in which case the big negative value is possibly what you want.
> See details in the docs for binascii and struct to learn more.

D'oh!  Correction: using the "<f" form produces, of course, the
result which assumes the input is *little-endian*.  Sorry for
the reversal.  The latter value, and what Alex showed as the
probable result, is likely what you want.


More information about the Python-list mailing list