/*note the output paramater pointers*/
void extract(uint input, uint *whole, uint *fraction){
*whole = (input >> 16);
*fraction = input & 0x00000000FFFFFFFF;
return;
}
float fix2float(int in)
{
int upper=in >> 16;
int lower=in & 0xFFFF;
return ((float)upper) + ((float) lower)/0x10000;
}
PS everyone stop beating up on cogat and read his last line.float fix2float(int in)
{
int upper=in >> 16;
int lower=in & 0xE000; // not 0xD000, nicwolff
if(lower >= 0x8000)
--upper;
return ((float)upper) + ((float) lower)/0x10000;
}
But then, it's accurate only to 2°C, so you could probably just use the top word only. The device is capable of reading temperatures below 0°C in which case the top word is a 2's complement signed number. Right-shifting this as a signed integer will sign-fill and preserve the value that you want. Masking the bottom half will make it non-negative, also the desired outcome.
Or, you could do it the way a programmer would: get it out of that useless base-10 notation and write it out as hex. Now the first four digits are the high word and the second four are the low word.
posted by Mars Saxman at 11:03 AM on October 22, 2005