python 32 bit float conversion
<p>Python 2.6 on Redhat 6.3</p>

<p>I have a device that saves 32 bit floating point value across 2 memory registers, split into most significant word and least significant word.
I need to convert this to a float.
I have been using the following code found on SO and it is similar to code I have seen elsewhere </p>

<pre><code>#!/usr/bin/env python
import sys
from ctypes import *
first  = sys.argv[1]
second = sys.argv[2]

reading_1 = str(hex(int(first)).lstrip("0x"))
reading_2 = str(hex(int(second)).lstrip("0x"))

sample = reading_1 + reading_2

def convert(s):
    i = int(s, 16)                   # convert from hex to a Python int
    cp = pointer(c_int(i))           # make this into a c integer
    fp = cast(cp, POINTER(c_float))  # cast the int pointer to a float pointer
    return fp.contents.value         # dereference the pointer, get the float

print convert(sample)
</code></pre>

<p>an example of the register values would be ;</p>

<p>register-1;16282 register-2;60597</p>

<p>this produces the resulting float of </p>

<p>1.21034872532</p>

<p>A perfectly cromulent number, however sometimes the memory values are something like;</p>

<p>register-1;16282 register-2;1147</p>

<p>which, using this function results in a float of;</p>

<p>1.46726675314e-36</p>

<p>which is a fantastically small number and not a number that seems to be correct. This device should be producing readings around the 1.2, 1.3 range.</p>

<p>What I am trying to work out is if the device is throwing bogus values or whether the values I am getting are correct but the function I am using is not properly able to convert them.</p>

<p>Also is there a better way to do this, like with numpy or something of that nature?
I will hold my hand up and say that I have just copied this code from examples on line and I have very little understanding of how it works, however it seemed to work in the test cases that I had available to me at the time.</p>

<p>Thank you.</p>
