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:
Marcos_Eztronic_1-1732282497069.png
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
Marcos_Eztronic_2-1732282634455.png
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);
static uint32_t DeserializeUint32(const uint8_t *buffer)
{
int value = 0;
value += (int)buffer[3] << 24U;
value += (int)buffer[2] << 16U;
value += (int)buffer[1] << 8U;
value += (int)buffer[0];
return value;
}
the returned value to dummyInt is OK:
Marcos_Eztronic_3-1732282807587.png
The float value, before cellParams.underVoltage = (float)(dummyInt), is:
Marcos_Eztronic_4-1732282862589.png
And after cellParams.underVoltage = (float)(dummyInt), is:
Marcos_Eztronic_5-1732283003922.png
but if I check the variable (not in memory monitor), the hex value looks fine, but the float value lookw incorrect:
Marcos_Eztronic_6-1732283097162.png
PS.: I tried atof, memcpy, not success.
Could someone help me? Thanks in advance!