[solved] Casting float to byte array

Hello everyone.

I’m having trouble creating an byte array for data transmission. The payload consists of a tag, a float value and some metainformation ending by ‘\0’

#include <Arduino.h>

void setup() {
  Serial.begin(9600);

  float f = 23.56;

  byte* b = (byte*) &f;

  byte buf[32] = {0};

  byte* msg = "master=";
  memcpy(buf,msg,7);
  strcat(buf,b);
  strcat(buf,"XXX");
  strcat(buf,'\0');


  Serial.print("buf = ");
  for(byte i=0;i<sizeof(buf);i++) {
    Serial.print(buf[i]); Serial.print(" ");
    if(buf[i]=='\0') {
      break;
    }
  } Serial.println("");


}


void loop() {


  delay(2000);
}

I copied this code principle from another part of my project where I do the exact same thing, but in this case the float value is not cast correctly.

Thanks for the help

What were you expecting to see printed? What actually got printed?

If you just print out the bytes that make up the float, they won't be recognizable as the float you encoded. Do you know how floating point numbers are encoded?

the array is of size 32, Which should be enough for a 4byte int and some characters.

byte buf[32] = {0};

the output reads:

109 97 115 116 101 114 61 225 122 188 65 88 88 88 0

where 61='=' and 88='X'. So the array apparently holds all the information, but the float conversion is wrong.

@delat_G: I also tried to cast the array between 61 and 88 into

float f = 23.56;
byte b* = (byte*) &f;

xbuf[4] = {0};
memcpy(xbuf,b,sizeof(xbuf)};

double x = atof(xbuf);

But the result is '0.00'

*b is not pointing to a string, so you can't use strcat() on it.

byte b* = (byte*) &f;
double x = atof(xbuf);

Aha, there's your problem. You're expecting that cast to turn your float into ASCII. That is NOT what is going to happen. The cast will just get you the four bytes that encode the float. It will not be recognizable to you as the number you started with.

If you want your float turned into ASCII then use dtostrf, not a cast.

https://en.wikipedia.org/wiki/IEEE_754

https://www.h-schmidt.net/FloatConverter/IEEE754.html

After copying the output I had a feeling about the 4(!) ‘strange’ bytes between the ‘=’ and the ‘XXX’

Is there a library function to convert byte[4] → float or do I need loop over the array and shifting each byte?

My search only got me to atof and strdof() but the dont ‘converse’ a real 4byte array

v3xX: My search only got me to atof and strdof() but the dont 'converse' a real 4byte array

Those are for ascii. If you just want 4 bytes of ieee754 back into a float then put them in an array and use a cast just like how you got them to byte in the first place.

float *fp =(float*) xbuf;


Serial.print(*fp);

Got it.

My slave will look for the '=' and the next 4bytes are the value.

My conversion now looks like

float x = *((float*)(xbuf));

the result is as excpected: 23.56

Thanks for the help!

P.s.: @Delta_G: https://www.h-schmidt.net/FloatConverter/IEEE754.html is a nice little tool -> saved to bookmarks