Help need with converting Hex string to IEEE format float

Richard Brodie R.Brodie at rl.ac.uk
Thu Dec 16 06:08:03 EST 2004


<i_vincent at hotmail.com> wrote in message
news:1103038606.112242.106700 at f14g2000cwb.googlegroups.com...

> 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

You can't fit 0x80000000L into a signed 32-bit integer, use 'L' for
an unsigned one.

> 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.

This depends on the Python version: before 2.4 0x80000000
corresponds to the bit pattern you expect but will be negative.
That's why it's happy as a signed 32-bit integer.

>>> 0x80000000 == 0x80000000L
<interactive input>:1: FutureWarning: hex/oct constants > sys.maxint
 will return positive values in Python 2.4 and up
...
False





More information about the Python-list mailing list