473,383 Members | 1,717 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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:

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. }
  11.  
Dec 17 '10 #1
7 14395
ah found the result:
Expand|Select|Wrap|Line Numbers
  1.  
  2. // read an unsigned long int from 4 bytes in buffer,
  3. // starting at offset, MSB first
  4. float readfloat(byte * buffer, int offset) {
  5.  
  6.   float val=0;
  7.  
  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);
  14.  
  15.   return val;
  16. }
  17.  
Dec 18 '10 #2
donbock
2,426 Expert 2GB
This is entirely nonportable. Where do those four bytes come from?
Dec 19 '10 #3
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
donbock
2,426 Expert 2GB
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
donbock
2,426 Expert 2GB
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
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
donbock
2,426 Expert 2GB
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

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

Similar topics

2
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
2
by: Chi Tang | last post by:
Hi, I try to convert a string to a float but it alway comes out with extra value. For example, the string input is '12.6' but the output is '12.6000003814697' The following is my code to do...
8
by: Kenny ODell | last post by:
I do not know how to convert from a byte array to a float, and back again. I read data from a serial port into a byte (entire command structure which I parse). I am able to sift the data and...
3
by: Aykut | last post by:
Hi I have to convert byte value to float in C#.Float is in IEEE 754 format. byte value is= 70 23 22 195 in a byte array. and its float value is = -150.0909 what is the algoritm for this...
0
by: GDB500 | last post by:
Can you help with code to convert from 64bit to 32bit other than just A=B :confused: The full story is that I have a 64bit float in hexidecimal so first I need to get this into a 64bit float ...
1
by: Jamie Risk | last post by:
I have a four (4) byte array that follows the IEEE 754 format. (It arrives on the windows platform from an embedded system via USB). How can I extract/convert/assign the encoded array to a...
1
by: tomerd11 | last post by:
please halp me to convert string to float
7
by: Mario M. Mueller | last post by:
Hi, I have a binary file containing 3 byte float values (big endian). How can I read them into python? The struct module does not work, since it expects 4 byte floats. Any hints? Mario
7
by: Guido van Brakel | last post by:
Hello I have this now: It now gives a int, but i would like to see floats. How can integrate that into the function? Regards,
4
by: Mason | last post by:
I have tried and tried... I'd like to read in a binary file, convert it's 4 byte values into floats, and then save as a .txt file. This works from the command line (import struct); In : f =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.