473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 12187
<i_*******@hotmail.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_*******@hotmail.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_hex(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.fromstring("F", (1, 1), data, "hex", "F;32BF").getpixel((0, 0))

</F>

Jul 18 '05 #5
On Tue, 14 Dec 2004 16:57:02 +0100, rumours say that "Fredrik Lundh"
<fr*****@pythonware.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).decode("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).decode("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.unhexlify('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*******@dread12.news.tele.dk:

##
st = '80 00 00 00'

import binascii
import struct

s = ''.join([binascii.a2b_hex(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
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...
2
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;...
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...
18
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...
2
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...
2
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...
116
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...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.