424,946 Members | 697 Online
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

# How to convert 4 byte's to float?

 P: 4 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: Expand|Select|Wrap|Line Numbers // 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; }   Dec 17 '10 #1
7 Replies

 P: 4 ah found the result: Expand|Select|Wrap|Line Numbers   // 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; }   Dec 18 '10 #2

 Expert 100+ P: 2,396 This is entirely nonportable. Where do those four bytes come from? Dec 19 '10 #3

 P: 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,&result, sizeof val); The 4 bytes in the buffer came from an utp buffer. Dec 19 '10 #4

 Expert 100+ P: 2,396 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). Dec 19 '10 #5

 Expert 100+ P: 2,396 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. Dec 22 '10 #6

 P: 4 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. Dec 22 '10 #7

 Expert 100+ P: 2,396 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. Dec 22 '10 #8