LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert unsigned integer to IEEE float

Solved!
Go to solution

Hello,

 

Is there a typecast function in CVI that would convert 2 unsigned integers to IEEE loat?

 

eg:

 

1st value MSB:  50588

2nd value LSB: 16425

 

Actual reading:  -5000.02

 

Thanks.

TN

 

0 Kudos
Message 1 of 3
(4,595 Views)
Solution
Accepted by topic author ThaiNg

You just need to do some pointer manipulation to build your float.

 

But you need to be aware of the size of the data types you're using.  Based on your example values, it looks like you are using the IEEE single precision format, which is 4 bytes.  In 32 bit operating systems, the unsigned int is also 4 bytes.  So you can't combine two unsigned int into an IEEE single without casting them as short ints, which are 2 bytes each.  You would need to check if the value in your unsigned int will fit in an unsigned short before casting it.

 

You can play with the sizeof() function to see the data type sizes on whatever platform you're using.

 

Here are a few lines of code the combine your example values to produce the expected result.

 

#include <ansi_c.h>
#include <utility.h>
main()
{
 // allocate room for myFloat (IEEE single precision)
 float myFloat;
 
 // create pointers to the two words in the IEEE single precision format
 unsigned short *pMSW, *pLSW;
 
 // move the pointers to the first and second word in your float
 pLSW = (unsigned short *) &myFloat;
 pMSW = pLSW +1;
 
 // initialize the word values
 *pMSW = 50588;
 *pLSW = 16425;
 
 // print everything out
 printf("MSW: %d\tLSW: %d\tIEEE Single: %f\n", *pMSW, *pLSW, myFloat);
 
 // wait for a response
 printf("Press any key to continue...\n");
 GetKey();
 
}

 

 

Here's a link to a discussion on going the other way, which has a sample program and links to more IEEE format info.

http://forums.ni.com/t5/LabWindows-CVI/How-to-convert-a-number-to-32-bit-binary-or-Hex/m-p/977159#M4...

Message 2 of 3
(4,579 Views)

Smiley HappyThank you very much. 

0 Kudos
Message 3 of 3
(4,572 Views)