Hello all.
I am trying to convert some bytes that I receive from CAN to a float variable.
However, the result is not correctly. I tried to change from little endian to big endian, not works.
For example, I am receiving the data 0x40333333, it`s suposed to be 2.8, but the result is totally different.
The images below will help to explain the situation (the variable that I am focusing in this example is cellParams.underVoltage
- variable initialiazion
cellParams.underVoltage = 2.80;
- initialization confirmation:
So after the initialization, I will send a CAN command to change the value (I will send the same value)
The new value is in the image below, from buffer position 3 until 6
When I receive the buffer, I try to do the following:
int dummyInt = DeserializeInt32(&cmdMsg->data[3]);
//cellParams.underVoltage = atof(&cmdMsg->data[3]); (same problem...)
cellParams.underVoltage = (float)(dummyInt);
The float value, before cellParams.underVoltage = (float)(dummyInt), is:
And after cellParams.underVoltage = (float)(dummyInt), is:
but if I check the variable (not in memory monitor), the hex value looks fine, but the float value lookw incorrect:
PS.: I tried atof, memcpy, not success.
Could someone help me? Thanks in advance!
Solved! Go to Solution.
Hello, thank you so much for the answer.
Memcpy not worked for me, but I found another solution (thats similar to memcpy, but that`s the onlyone that worked...)
Hi,
I tried it on my side and only working method for me is memcpy - like this:
unsigned int counter = 0x40333333;
float f=0.0;
memcpy(&f,&counter,4);
Hello, thank you so much for the answer.
Memcpy not worked for me, but I found another solution (thats similar to memcpy, but that`s the onlyone that worked...)