IEEE 754 floats

Tom B. sbabbitt at commspeed.net
Tue Sep 14 16:10:54 CEST 2004


"Dale Huffman" <dale_huffman at steris.com> wrote in message 
news:a2a325a7.0409140504.2d36562b at posting.google.com...
> Is there a simple way to convert an IEEE-754 floating point ascii
> string ( "0x40400000" = 3.0, 32bit ) into a float variable, without
> writing a function to do the math.  I have transferred this across a
> network from a device I have no contol over and it sends all data as a
> string.  Everything I have tried just converts from hex to decimal and
> adds a decimal point and a zero.
>
> string.atof("0x40400000")  returns 1077936128.0
>
> In case I'm not explaining clearly, what I'm looking for could be
> coded in C as follows:
>
> int     a = 0x40400000;
> float *ap = (float *)&a;
>
> float myFloat = *ap;
>
> Sorry if the C offeded anyone in the Py crowd but I'm new to Python
> and so far it rocks - I just don't have the basics down yet.

Have a look at the struct module in the standard distribution.

Tom 





More information about the Python-list mailing list