472,993 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 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 7041
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tom | last post by:
Has anyone ever seen a IComparer for floats the returns magnitude. i.e. instead of returning -1, it would return -5. To let you know HOW different the two numbers are. obviously for int it is a -...
8
by: Madhusudan Singh | last post by:
Is it possible to convert a very long list of strings to a list of floats in a single statement ? I have tried float(x) and float(x) but neither work. I guess I would have to write a loop if...
4
by: ej | last post by:
I ran into something today I don't quite understand and I don't know all the nitty gritty details about how Python stores and handles data internally. (I think) I understand why, when you type in...
1
by: GooglePoster | last post by:
Hello, I am looking for a function/utility to read in an IEEE value and convert this to decimal, for testing purposes. Also, I need to convert decimal values to IEEE values to send out on a...
2
by: Benjamin Rutt | last post by:
Does anyone have C code laying around to do this? I have to read in some binary data files that contains some 4-byte IBM/370 floating point values. I would like a function to convert 4-byte...
33
by: Nick Maclaren | last post by:
The numerical robustness of Python is very poor - this is not its fault, but that of IEEE 754 and (even more) C99. In particular, erroneous numerical operations often create apparently valid...
13
by: yb | last post by:
Hi, Is there a CSS method to clear a float such that it aligns with the left content edge. For example: X X X X X X X X
16
by: luca bertini | last post by:
Hi, i have strings which look like money values (ie 34.45) is there a way to convert them into float variables? everytime i try I get this error: "numb = float(my_line) ValueError: empty string...
0
by: Matthieu Brucher | last post by:
2008/11/5 L V <somelauw@yahoo.com>: Hi, I don't think the Python developers list is th best list to post this kind of question. What version of Python did you use for this test? Matthieu
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.