IEEE 754 floats

D

Dale Huffman

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.
 
P

Peter Otten

Dale said:
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.
struct.unpack("f", struct.pack("l", int("0x40400000", 16)))[0]
3.0

There may be simpler ways, though.

Peter
 
T

Tom B.

Dale Huffman said:
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
 
R

Robert Kern

Dale said:
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.

Take a look at the struct module.

E.g.

In [6]: import struct

In [7]: s = struct.pack('i', 0x40400000)

In [8]: s
Out[8]: '@@\x00\x00'

In [9]: struct.unpack('f', s)
Out[9]: (3.0,)

You might have to worry about endian-related issues, of course, but the
struct module allows you to handle the various cases.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
D

Dale Huffman

Thanks everyone it works... but does this seem like a kludgy (sp?) way
to do this, or have I just not gotten to PythonThink mode yet.
 
R

Robert Kern

Dale said:
Thanks everyone it works... but does this seem like a kludgy (sp?) way
to do this, or have I just not gotten to PythonThink mode yet.

I would submit that the task of converting an int to a float like that
is a fairly rare task. It is exceedingly more common to convert a
sequence of bytes to and from ints/floats. So Python opts for the more
general solution, which happens to also be the best solution for the
most common case.

Additionally, the fine details of memory layout for basic types like
ints and floats *ought* to be hidden from the user. Of course, you can
still get at them if you need to via the struct module.

If you run into this situation a lot, it's easy enough to write a
function that encapsulates the kludge.

For getting into PythonThink mode, type "import this" at the interactive
prompt.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top