Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My floating value doesn't match my value in C

I'm trying to interface a board with a raspberry. I have to read/write value to the board via modbus, but I can't write floating point value like the board.

I'm using C, and Eclipse debug perspective to see the variable's value directly. The board send me 0x46C35000 which should value 25'000 Dec but eclipse shows me 1.18720512e+009...

When I try on this website http://www.binaryconvert.com/convert_float.html?hexadecimal=46C35000 I obtain 25,000.

What's the problem?

For testing purposes I'm using this:

int main(){
    
    while(1){ // To view easily the value in the debug perspective 
        float test = 0x46C35000;
        printf("%f\n",test);
    }
    return 0;
}

Thanks!

like image 425
Wireless Learning Avatar asked Dec 10 '22 02:12

Wireless Learning


1 Answers

When you do this:

float test = 0x46C35000;

You're setting the value to 0x46C35000 (decimal 1187205120), not the representation.

You can do what you want as follows:

union {
    uint32_t i;
    float f;
} u = { 0x46C35000 };

printf("f=%f\n", u.f);

This safely allows an unsigned 32-bit value to be interpreted as a float.

like image 163
dbush Avatar answered Dec 26 '22 11:12

dbush