LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I interpret a 4-byte Integer as a IEEE 754 float?

Maybe a quite trivia Problem: Let's say I have 2 variables: unsigned int RAW and float DATA.
Now I want to interpret the 4-byte Integer as a IEEE 754 float. Is there a single command like Fmt or Scan with a corresponding Format String?
Not to be misunderstood: I need to interpret the 4 integer bytes as float, not just convert an Integer to float.

Thanks for any help
0 Kudos
Message 1 of 4
(4,011 Views)
I think you can do what you want by using an union.

union
{
in raw;
float data;
} myTriviaProblem;

Save the number as myTrivialProblem.raw, then read it back as myTrivialProblem.data.

This might not be "C legal" and might not work the same on different compilers.

I have used something like this when reading data from devices that where sending floating point numbers encoded as bytes.

Omar
0 Kudos
Message 2 of 4
(4,014 Views)
I'm not sure about the IEEE754 formatting, but I had a somewhat similar problem reading in 4-byte ints from a device that transmitted the ints directly in Hex format (four-byte int, 2's complement, least sig. byte first). Have you tried the [z] format code? That is,

Scan(scan_buffer, "%*i[z]>%*f", bytes_to_read, bytes_to_read, fvals );

where the bytes_to_read is 4*num_of_ints_in_array, and fvals is an array of floats.

Your application sounds a little different, but I thought I'd throw this idea out there. If it works, it's a nice one-line solution.
0 Kudos
Message 3 of 4
(4,011 Views)
Thanks for your ideas. I deceided to use the Union-Solution. It's not what I call clean and straight programming, but hey, it works perfectly. 🙂

Thanks again!
0 Kudos
Message 4 of 4
(4,011 Views)