Help need with converting Hex string to IEEE format float

2004-12-13 Thread i_vincent
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

Re: Help need with converting Hex string to IEEE format float

2004-12-13 Thread Fredrik Lundh
<[EMAIL PROTECTED]> 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 gen

Re: Help need with converting Hex string to IEEE format float

2004-12-14 Thread Max M
[EMAIL PROTECTED] 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.

Re: Help need with converting Hex string to IEEE format float

2004-12-14 Thread Fredrik Lundh
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)) -- http://mail.python.org/mailman/listinfo/python-list

Re: Help need with converting Hex string to IEEE format float

2004-12-14 Thread Fredrik Lundh
># 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

Re: Help need with converting Hex string to IEEE format float

2004-12-15 Thread TZOTZIOY
On Tue, 14 Dec 2004 16:57:02 +0100, rumours say that "Fredrik Lundh" <[EMAIL PROTECTED]> 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 abo

Re: Help need with converting Hex string to IEEE format float

2004-12-15 Thread Peter Hansen
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' >>>

Re: Help need with converting Hex string to IEEE format float

2004-12-16 Thread Richard Brodie
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > 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 0x8000L into a signed 32-bit integer, use 'L' for an unsigned one.

Re: Help need with converting Hex string to IEEE format float

2004-12-17 Thread Ian Vincent
Max M <[EMAIL PROTECTED]> wrote in news:41bf121e$0$280 [EMAIL PROTECTED]: > > ## > 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.