473,587 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help need with converting Hex string to IEEE format float

Hi all,

Newbie Python programmer here, so please be patient. I have spent all
day googling for an answer to my problem, but everything I try fails to
work (or works from the Interpreter with a set value but not from my
code with dynamic values).

Okay, here is the general gist of the problem. I am using Python to
parse an output file (from a MAK Logger but that is not really
important). Now I have some data that is contained on a line in this
file like:

80 00 00 00

Each of these numbers is a Hex byte making up a four byte (32 bit
Big-Endian) IEEE float. I have read this data into Python using
readlines and then line.split(). This gives me:

['80', '00', '00', '00']

I am then reading these in as:

wib[0] + wib[1] + wib[2] + wib[3] = 8000000 as a string

Now this is the point where I get stuck, I have tried various ways of
implementing the pack/unpack methods of the struct module but with no
luck.

One example I tried was:

wibble = struct.unpack(" f", struct.pack("l" , long(conv_str, 16)))
OverflowError: long int too large to convert to int

If I follow the examples I have found on the net using a set value of
0x80000000 in them, everything works fine from the interpreter line.

Arrrggggghhhh.

Everything has worked really easily up this point but I am now stuck
completely, I would be grateful for any help people can offer.
Regards

Ian Vincent

Jul 18 '05 #1
7 12221
<i_*******@hotm ail.com> wrote:
Newbie Python programmer here, so please be patient. I have spent all
day googling for an answer to my problem, but everything I try fails to
work (or works from the Interpreter with a set value but not from my
code with dynamic values).

Okay, here is the general gist of the problem. I am using Python to
parse an output file (from a MAK Logger but that is not really
important). Now I have some data that is contained on a line in this
file like:

80 00 00 00

Each of these numbers is a Hex byte making up a four byte (32 bit
Big-Endian) IEEE float. I have read this data into Python using
readlines and then line.split(). This gives me:

['80', '00', '00', '00']


how about:

# convert to byte string
import struct
s = "".join([chr(int(c, 16)) for c in x])
v = struct.unpack(" !f", s)

or

# convert to byte string, via the array module
import array, struct
a = array.array("B" , [int(c, 16) for c in x])
v = struct.unpack(" !f", )

</F>

Jul 18 '05 #2
> # convert to byte string, via the array module
import array, struct
a = array.array("B" , [int(c, 16) for c in x])
v = struct.unpack(" !f", )


eh? should be:

# convert to byte string, via the array module
import array, struct
a = array.array("B" , [int(c, 16) for c in x])
v = struct.unpack(" !f", a)

</F>

Jul 18 '05 #3
i_*******@hotma il.com wrote:
Each of these numbers is a Hex byte making up a four byte (32 bit
Big-Endian) IEEE float. I have read this data into Python using
readlines and then line.split(). This gives me:

['80', '00', '00', '00']

Oh, programmers loves this kind stuff. You should get tons of answers.

##
st = '80 00 00 00'

import binascii
import struct

s = ''.join([binascii.a2b_he x(s) for s in st.split()])
v = struct.unpack(" f", s)[0]
print v
##

regards Max M

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
Jul 18 '05 #4
Max M wrote:
Oh, programmers loves this kind stuff. You should get tons of answers.


data = '80 00 00 00'

import Image
v = Image.fromstrin g("F", (1, 1), data, "hex", "F;32BF").getpi xel((0, 0))

</F>

Jul 18 '05 #5
On Tue, 14 Dec 2004 16:57:02 +0100, rumours say that "Fredrik Lundh"
<fr*****@python ware.com> might have written:
how about:

# convert to byte string
import struct
s = "".join([chr(int(c, 16)) for c in x])
v = struct.unpack(" !f", s)


I think that the third line in the snippet above could also be:

s = "".join(x).deco de("hex")

I am not sure I remember in which version of Python the hex codec was
added, but it is handy.
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #6
Christos TZOTZIOY Georgiou wrote:
s = "".join(x).deco de("hex")

I am not sure I remember in which version of Python the hex codec was
added, but it is handy.


Of course, binascii could do this since 2.0 or so, but not
having to import another module *is* nice:
'ff12'.decode(' hex') '\xff\x12'
import binascii
binascii.unhexl ify('ff12')

'\xff\x12'

Thanks for pointing it out, Christos.
Jul 18 '05 #7
Max M <ma**@mxm.dk> wrote in news:41bf121e$0 $280
$e*******@dread 12.news.tele.dk :

##
st = '80 00 00 00'

import binascii
import struct

s = ''.join([binascii.a2b_he x(s) for s in st.split()])
v = struct.unpack(" f", s)[0]
print v
##


This one worked great for what I was trying to do.

Thanks to everybody for your help.

TTFN

Ian
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
6672
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual bytes. I've tried using bitwise operators, but they seem to convert the value into an integer first, and i've tried using the toString() method to...
2
3625
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats; http://groups.google.com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe#4aadc71be8aeddbe I copied this code and...
2
15836
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 IBM/370 float values into IEEE 754 'float' values that I can use directly with any modern C environment (that uses IEEE 754). I can handle endian...
18
3346
by: James Radke | last post by:
Hello, We are currently using a user DLL that when working in VB 6.0 has a user defined type as a parameter. Now we are trying to use the same DLL from a vb.net application and are having some problems getting it to work and we don't know why. Basically the function is accepting the parameters, and then returning an error and never...
2
2344
by: James Radke | last post by:
Hello, I have a vb.net windows application that is calling an older DLL provided by a third party. They supplied a VB 6 application that, when run on my systemn successfully passes data to the database. The problem I am having is that when I pass the data from VB.NET, the fields defined as double all contain strange values. For example,...
2
3447
by: Madhusudhanan Chandrasekaran | last post by:
Hi all: When I try to convert a float variable into string via repr() or str() function, i get the value as is, i.e '0.1e-7' in IEEE 754 format. Instead how can force the created string to represent the floating point in non-scientific fashion (non IEEE 754 format)? i.e something like 0.000000001 Thanks in advance for your help.
116
35783
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused problems elsewhere in another part of the system where that figure was used for some calculation and some eventual truncation led to the system going...
28
5851
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is...
4
4793
by: Yasin Cepeci | last post by:
I ve get float data from serial port. I ve taken it in the form of hex by modbus protocol. I know it is float but I couldnt convert it there is a few sample data below; B3 33 43 34 = 180.699997 33 33 43 33 = 179.199997 B3 33 43 34= 180.699997 CC CD 43 33=179.800003 But how can I found it. I couldnt resolve it.
0
7918
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5713
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.