Single precision big endian float values to Python's float (double precision, big endian)
Question: I need to receive hex encoded single precision big endian float values coming
from an Arduino over a serial line (RS-232). How do convert them to Python's
float which are big endians with double precision?

The Arduino send something like "8192323E" and in Python I would like to have
0.174387. I found "[Convert hex to
float](http://stackoverflow.com/questions/1592158/python-convert-hex-to-
float)" but it seems that all of them don't work for single precision floats.

From the linked page, this looks promising:

    
    
    from ctypes import *
    
    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
    

But it still doesn't work with my single precision floats.

In Java
([Processing](http://en.wikipedia.org/wiki/Processing_%28programming_language%29))
I've been able to do that:

    
    
    float decodeFloat(String inString) {
      byte [] inData = new byte[4];
    
      inString = inString.substring(2, 10); // discard the leading "f:"
      inData[0] = (byte) unhex(inString.substring(0, 2));
      inData[1] = (byte) unhex(inString.substring(2, 4));
      inData[2] = (byte) unhex(inString.substring(4, 6));
      inData[3] = (byte) unhex(inString.substring(6, 8));
    
      int intbits = (inData[3] << 24) | ((inData[2] & 0xff) << 16) | ((inData[1] & 0xff) << 8) | (inData[0] & 0xff);
      //unhex(inString.substring(0, 8));
      return Float.intBitsToFloat(intbits);
    }
    

For your reference, this is the C code running on the Arduino implementing the
hex encoding.

    
    
    void serialFloatPrint(float f) {
      byte * b = (byte *) &f;
      Serial.print("f:");
      for(int i=0; i<4; i++) {
    
        byte b1 = (b[i] >> 4) & 0x0f;
        byte b2 = (b[i] & 0x0f);
    
        char c1 = (b1 < 10) ? ('0' + b1) : 'A' + b1 - 10;
        char c2 = (b2 < 10) ? ('0' + b2) : 'A' + b2 - 10;
    
        Serial.print(c1);
        Serial.print(c2);
      }
    }
    


Answer: Building on [Ignacio Vazquez-Abrams's
answer](http://stackoverflow.com/questions/4315190/single-precision-big-
endian-float-values-to-pythons-float-double-precision-big/4315230#4315230),

    
    
    import binascii
    import struct
    
    text='8192323E'
    print(struct.unpack('<f',binascii.unhexlify(text))[0])
    # 0.17438699305057526
    

