how to convert 4 bytes into a float ?

Alex Martelli aleaxit at yahoo.com
Tue Feb 8 05:49:58 EST 2005


Jean-Baptiste PERIN <jb_perin at yahoo.fr> wrote:

> I read 4 bytes from a binary file.
> 
> These bytes represent a floating point number (mantisse exponent form)
> 
> How can I get a float from these bytes ?

See the docs for module struct, specifically the struct.unpack function,
if the float is in the binary format your machine expects (or possibly
that but with the wrong endianity).  If the float's binary format is
different from what your machine uses, you're in for a lot of painful
bit-twiddling.


Alex



More information about the Python-list mailing list