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 full description of the format (which apparently is standard for C) is given in this document.

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, which works fine for positives, but outputs incorrect data for negative numbers:

int Convert(BYTE msg0, BYTE msg1, float Target){

    // Combine the 2 bytes 
    unsigned int F = msg0*256 + msg1;

    // display the combined hex bytes and the decimal equivilant
    // These values are correct
    printf("\n\n Combined F = 0x%x \t F = %i",F,F);

    double Ff = (double)F/(32767); // ((2^15)-1) = 32767 = 0x7FFF

    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!

So it was just a couple of tiny things to change :-)

short F = msg0*256 + msg1;
double Ff = (double)F/(32768); // ((2^15)-1) = 32767 = 0x7FFF
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.