By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 697 Online
Bytes IT Community
+ Ask a Question
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
  1. // read an unsigned long int from 4 bytes in buffer,
  2. // starting at offset, MSB first
  3. unsigned long readLongInt(byte * buffer, int offset) {
  4.   unsigned long result=0;
  5.   result |= ((unsigned long)(buffer[offset]) << 0x18);
  6.   result |= ((unsigned long)(buffer[offset+1]) << 0x10);
  7.   result |= ((unsigned long)(buffer[offset+2]) << 0x08);
  8.   result |= ((unsigned long)(buffer[offset+3]));
  9.   return result;
  10. }
Dec 17 '10 #1
Share this Question
Share on Google+
7 Replies

P: 4
ah found the result:
Expand|Select|Wrap|Line Numbers
  2. // read an unsigned long int from 4 bytes in buffer,
  3. // starting at offset, MSB first
  4. float readfloat(byte * buffer, int offset) {
  6.   float val=0;
  8.   unsigned long result=0;
  9.   result |= ((unsigned long)(buffer[offset]) << 0x18);
  10.   result |= ((unsigned long)(buffer[offset+1]) << 0x10);
  11.   result |= ((unsigned long)(buffer[offset+2]) << 0x08);
  12.   result |= ((unsigned long)(buffer[offset+3]));
  13.   memcpy(&val,&result,4);
  15.   return val;
  16. }
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

Post your reply

Sign in to post your reply or Sign up for a free account.