10-13-2010 11:53 AM
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
Solved! Go to Solution.
10-13-2010 01:24 PM - edited 10-13-2010 01:25 PM
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.
10-13-2010 01:53 PM
Thank you very much.