Help need with converting Hex string to IEEE format float

i_vincent at hotmail.com i_vincent at hotmail.com
Tue Dec 14 10:36:46 EST 2004


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




More information about the Python-list mailing list