Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help need with converting Hex string to IEEE format float

Reply
Thread Tools

Help need with converting Hex string to IEEE format float

 
 
i_vincent@hotmail.com
Guest
Posts: n/a
 
      12-14-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

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-14-2004
<(E-Mail Removed)> 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>



 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-14-2004
> # 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>



 
Reply With Quote
 
Max M
Guest
Posts: n/a
 
      12-14-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      12-14-2004
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>



 
Reply With Quote
 
Christos TZOTZIOY Georgiou
Guest
Posts: n/a
 
      12-15-2004
On Tue, 14 Dec 2004 16:57:02 +0100, rumours say that "Fredrik Lundh"
<(E-Mail Removed)> 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 RFC195
I really should keep that in mind when talking with people, actually...
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      12-15-2004
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.
 
Reply With Quote
 
Richard Brodie
Guest
Posts: n/a
 
      12-16-2004

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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


 
Reply With Quote
 
Ian Vincent
Guest
Posts: n/a
 
      12-17-2004
Max M <(E-Mail Removed)> wrote in news:41bf121e$0$280
$(E-Mail Removed):
>
> ##
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments