Hi Everyone,
I'm trying to interpret some data from an embedded sensor device into a C++ program. The device uses a numerical format called 1.15 fractional format. This means it transmits 2 hex bytes into a value rangle of ~1 < n < -1.
A description of the format is given in this document (
http://phobos.iet.unipi.it/~pieri/EdT2/2181_Manual/Appendix_C.pdf[
^]).
I can understand how the format works (for example, negative values have a positive 15th bit) but I'm struggling to implement a convertor in C++ that can identify when the incoming data is negative.
So far I have this function, but it outputs incorrect data for negative numbers
int Convert(BYTE msg0, BYTE msg1, float Target){
unsigned int F = msg0*256 + msg1;
printf("\n\n Combined F = 0x%x \t F = %i",F,F);
double Ff = (double)F/(32767);
printf("\n Ff = %f",Ff);
printf("\n Target = %f",Target);
return 0;
}
The Target values are taken from the sensor documentation. As you can see my Positive outputs are pretty close (but not perfect) yet the negative values are way off.
Target______Hex_______Output
0.9999_____0x7FFF____1.000000
0.5_________0x4000____0.500015
-0.5________0xC000____1.500046
-1.0________0x8000____1.000031
Any suggestions greatfully recieved, I'm a little out of my depth here!