How to convert 4 byte's to float?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Marijn Holtrop
    New Member
    • Dec 2010
    • 4

    How to convert 4 byte's to float?

    I'm adapting a piece of code. The original code takes 4 bytes and convert that to an integer.
    In my case this memory contains a float and I want the function to return a float.
    Here is the original code:

    Code:
    // read an unsigned long int from 4 bytes in buffer,
    // starting at offset, MSB first
    unsigned long readLongInt(byte * buffer, int offset) {
      unsigned long result=0;
      result |= ((unsigned long)(buffer[offset]) << 0x18);
      result |= ((unsigned long)(buffer[offset+1]) << 0x10);
      result |= ((unsigned long)(buffer[offset+2]) << 0x08);
      result |= ((unsigned long)(buffer[offset+3]));
      return result;
    }
  • Marijn Holtrop
    New Member
    • Dec 2010
    • 4

    #2
    ah found the result:
    Code:
    // read an unsigned long int from 4 bytes in buffer,
    // starting at offset, MSB first
    float readfloat(byte * buffer, int offset) {
    
      float val=0;
    
      unsigned long result=0;
      result |= ((unsigned long)(buffer[offset]) << 0x18);
      result |= ((unsigned long)(buffer[offset+1]) << 0x10);
      result |= ((unsigned long)(buffer[offset+2]) << 0x08);
      result |= ((unsigned long)(buffer[offset+3]));
      memcpy(&val,&result,4);
    
      return val;
    }

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      This is entirely nonportable. Where do those four bytes come from?

      Comment

      • Marijn Holtrop
        New Member
        • Dec 2010
        • 4

        #4
        I thought a float was always 4 bytes long. It would also be possible to test the length of val but the result is only 4 bytes long.

        memcpy(&val,&re sult, sizeof val);

        The 4 bytes in the buffer came from an utp buffer.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          The number of bytes in a float is implementation-dependent. It could change every time you switch to a different compiler (or a new version of the compiler you've already got).

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            When you say the four bytes come from a "utp buffer", do you mean a BitTorrent μTP (micro transport protocol) message?

            That would suggest that a program on some other computer converted a float into a four-byte sequence that was then sent to you via BitTorrent. If so, then your mission is to understand precisely what rules that other computer used to create the four bytes, taking into account that the C compiler on the other computer may have an entirely different encoding scheme for the float type.

            Comment

            • Marijn Holtrop
              New Member
              • Dec 2010
              • 4

              #7
              No it is not all that way so complicated. This runs inside a microcontroller and the message is transmitted by an iphone using the osc protocol (open sound control). It is all very well defined.
              Due to compatibillity with a specific iphone app I needed to convert the float. That app only supports the transmission of floats and my application could only handle integers so I needed a conversion.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                The OSC specification says that their float32 type is a "32-bit big-endian IEEE 754 floating point number". This likely corresponds to what IEEE 754 calls binary32. Follow the link for algorithms to convert an osc float32 to and from a C floating point variable.

                Comment

                Working...