471,084 Members | 1,033 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,084 software developers and data experts.

IEEE 754 floats

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.
Jul 18 '05 #1
5 6710
Dale Huffman wrote:
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

Jul 18 '05 #2

"Dale Huffman" <da**********@steris.com> wrote in message
news:a2**************************@posting.google.c om...
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
Jul 18 '05 #3
Dale Huffman wrote:
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #4
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.
Jul 18 '05 #5
Dale Huffman wrote:
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Tom | last post: by
8 posts views Thread by Madhusudan Singh | last post: by
33 posts views Thread by Nick Maclaren | last post: by
13 posts views Thread by yb | last post: by
16 posts views Thread by luca bertini | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.