Bytes to Float not working

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Bytes to Float not working

Jump to solution
1,067 Views
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 Kudos
Reply
1 Solution
997 Views
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.

View solution in original post

0 Kudos
Reply
2 Replies
1,007 Views
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 Kudos
Reply
998 Views
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 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2000140%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EBytes%20to%20Float%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2000140%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%20all.%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20convert%20some%20bytes%20that%20I%20receive%20from%20CAN%20to%20a%20float%20variable.%3C%2FP%3E%3CP%3EHowever%2C%20the%20result%20is%20not%20correctly.%20I%20tried%20to%20change%20from%20little%20endian%20to%20big%20endian%2C%20not%20works.%3C%2FP%3E%3CP%3EFor%20example%2C%20I%20am%20receiving%20the%20data%200x40333333%2C%20it%60s%20suposed%20to%20be%202.8%2C%20but%20the%20result%20is%20totally%20different.%3C%2FP%3E%3CP%3EThe%20images%20below%20will%20help%20to%20explain%20the%20situation%20(the%20variable%20that%20I%20am%20focusing%20in%20this%20example%20is%26nbsp%3B%3CSTRONG%3EcellParams.underVoltage%3C%2FSTRONG%3E%3CLI-EMOJI%20id%3D%22lia_disappointed-face%22%20title%3D%22%3Adisappointed_face%3A%22%3E%3C%2FLI-EMOJI%3E%3C%2FP%3E%3CP%3E-%20variable%20initialiazion%3C%2FP%3E%3CP%3EcellParams.underVoltage%20%3D%202.80%3B%3C%2FP%3E%3CP%3E-%20initialization%20confirmation%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Marcos_Eztronic_1-1732282497069.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Marcos_Eztronic_1-1732282497069.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F312030iEEBE591E30C0CC65%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Marcos_Eztronic_1-1732282497069.png%22%20alt%3D%22Marcos_Eztronic_1-1732282497069.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3ESo%20after%20the%20initialization%2C%20I%20will%20send%20a%20CAN%20command%20to%20change%20the%20value%20(I%20will%20send%20the%20same%20value)%3C%2FP%3E%3CP%3EThe%20new%20value%20is%20in%20the%20image%20below%2C%20from%20buffer%20position%203%20until%206%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-left%22%20image-alt%3D%22Marcos_Eztronic_2-1732282634455.png%22%20style%3D%22width%3A%20722px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Marcos_Eztronic_2-1732282634455.png%22%20style%3D%22width%3A%20722px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F312031i2932CAF28C1972E2%2Fimage-dimensions%2F722x640%3Fv%3Dv2%22%20width%3D%22722%22%20height%3D%22640%22%20role%3D%22button%22%20title%3D%22Marcos_Eztronic_2-1732282634455.png%22%20alt%3D%22Marcos_Eztronic_2-1732282634455.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CP%3EWhen%20I%20receive%20the%20buffer%2C%20I%20try%20to%20do%20the%20following%3A%3C%2FP%3E%3CP%3Eint%20dummyInt%20%3D%20DeserializeInt32(%26amp%3BcmdMsg-%26gt%3Bdata%5B3%5D)%3B%3CBR%20%2F%3E%2F%2FcellParams.underVoltage%20%3D%20atof(%26amp%3BcmdMsg-%26gt%3Bdata%5B3%5D)%3B%20(same%20problem...)%3CBR%20%2F%3EcellParams.underVoltage%20%3D%20(float)(dummyInt)%3B%3C%2FP%3E%3CDIV%3Estatic%20uint32_t%20DeserializeUint32(const%20uint8_t%20*buffer)%3C%2FDIV%3E%3CDIV%3E%7B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20int%20value%20%3D%200%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20value%20%2B%3D%20(int)buffer%5B3%5D%20%26lt%3B%26lt%3B%2024U%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20value%20%2B%3D%20(int)buffer%5B2%5D%20%26lt%3B%26lt%3B%2016U%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20value%20%2B%3D%20(int)buffer%5B1%5D%20%26lt%3B%26lt%3B%208U%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20value%20%2B%3D%20(int)buffer%5B0%5D%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3E%26nbsp%3B%20%26nbsp%3B%20return%20value%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%7D%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3Ethe%20returned%20value%20to%20dummyInt%20is%20OK%3A%3C%2FDIV%3E%3CDIV%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Marcos_Eztronic_3-1732282807587.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Marcos_Eztronic_3-1732282807587.png%22%20style%3D%22width%3A%20310px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F312034iB879995DC7B1F72E%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Marcos_Eztronic_3-1732282807587.png%22%20alt%3D%22Marcos_Eztronic_3-1732282807587.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CBR%20%2F%3E%3CP%3EThe%20float%20value%2C%20before%26nbsp%3BcellParams.underVoltage%20%3D%20(float)(dummyInt)%2C%20is%3A%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Marcos_Eztronic_4-1732282862589.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Marcos_Eztronic_4-1732282862589.png%22%20style%3D%22width%3A%2092px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F312037iE4BFA2216DD1B948%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Marcos_Eztronic_4-1732282862589.png%22%20alt%3D%22Marcos_Eztronic_4-1732282862589.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CBR%20%2F%3E%3CP%3EAnd%20after%26nbsp%3BcellParams.underVoltage%20%3D%20(float)(dummyInt)%2C%20is%3A%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Marcos_Eztronic_5-1732283003922.png%22%20style%3D%22width%3A%20632px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Marcos_Eztronic_5-1732283003922.png%22%20style%3D%22width%3A%20632px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F312038i3C94EBE926FE050C%2Fimage-dimensions%2F632x49%3Fv%3Dv2%22%20width%3D%22632%22%20height%3D%2249%22%20role%3D%22button%22%20title%3D%22Marcos_Eztronic_5-1732283003922.png%22%20alt%3D%22Marcos_Eztronic_5-1732283003922.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CBR%20%2F%3E%3CP%3Ebut%20if%20I%20check%20the%20variable%20(not%20in%20memory%20monitor)%2C%20the%20hex%20value%20looks%20fine%2C%20but%20the%20float%20value%20lookw%20incorrect%3A%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Marcos_Eztronic_6-1732283097162.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Marcos_Eztronic_6-1732283097162.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F312039i8DA1EE2D0425534F%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Marcos_Eztronic_6-1732283097162.png%22%20alt%3D%22Marcos_Eztronic_6-1732283097162.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3CBR%20%2F%3E%3CP%3EPS.%3A%20I%20tried%26nbsp%3B%3CSPAN%3Eatof%2C%20memcpy%2C%20not%20success.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3ECould%20someone%20help%20me%3F%20Thanks%20in%20advance!%3C%2FSPAN%3E%3C%2FP%3E%3CBR%20%2F%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2001037%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Bytes%20to%20Float%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2001037%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%2C%20thank%20you%20so%20much%20for%20the%20answer.%3C%2FP%3E%3CP%3EMemcpy%20not%20worked%20for%20me%2C%20but%20I%20found%20another%20solution%20(thats%20similar%20to%20memcpy%2C%20but%20that%60s%20the%20onlyone%20that%20worked...)%3C%2FP%3E%3CBR%20%2F%3E%3CDIV%3Estatic%20float%20DeserializeFloat(const%20uint8_t%20*buffer)%3C%2FDIV%3E%3CDIV%3E%7B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Euint32_t%20value%20%3D%200%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Evalue%20%7C%3D%20buffer%5B3%5D%20%26lt%3B%26lt%3B%2024U%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Evalue%20%7C%3D%20buffer%5B2%5D%20%26lt%3B%26lt%3B%2016U%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Evalue%20%7C%3D%20buffer%5B1%5D%20%26lt%3B%26lt%3B%208U%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Evalue%20%7C%3D%20buffer%5B0%5D%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Efloat%20f%20%3D%20*((float*)%26amp%3Bvalue)%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3Ereturn%20f%3B%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%7D%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EAnyway%2C%20thank%20you%20very%20much%20for%20your%20time.%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2000987%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ERe%3A%20Bytes%20to%20Float%20not%20working%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2000987%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%2C%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20tried%20it%20on%20my%20side%20and%20only%20working%20method%20for%20me%20is%20memcpy%20-%20like%20this%3A%26nbsp%3B%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3Eunsigned%20int%20counter%20%3D%200x40333333%3B%3CBR%20%2F%3Efloat%20f%3D0.0%3B%3CBR%20%2F%3Ememcpy(%26amp%3Bf%2C%26amp%3Bcounter%2C4)%3B%26nbsp%3B%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E