Bytes to Float not working

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Bytes to Float not working

跳至解决方案
830 次查看
Marcos_Eztronic
Contributor I

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!

 

0 项奖励
回复
1 解答
760 次查看
Marcos_Eztronic
Contributor I

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...)

 

static float DeserializeFloat(const uint8_t *buffer)
{
uint32_t value = 0;
 
value |= buffer[3] << 24U;
value |= buffer[2] << 16U;
value |= buffer[1] << 8U;
value |= buffer[0];
float f = *((float*)&value);
return f;
}
 
Anyway, thank you very much for your time.

在原帖中查看解决方案

0 项奖励
回复
2 回复数
770 次查看
jiri_kral
NXP Employee
NXP Employee

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); 

 

 

0 项奖励
回复
761 次查看
Marcos_Eztronic
Contributor I

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...)

 

static float DeserializeFloat(const uint8_t *buffer)
{
uint32_t value = 0;
 
value |= buffer[3] << 24U;
value |= buffer[2] << 16U;
value |= buffer[1] << 8U;
value |= buffer[0];
float f = *((float*)&value);
return f;
}
 
Anyway, thank you very much for your time.
0 项奖励
回复