[Solved] IEEE byte array to float publish

I try to convert an array of byte to a float value.

I found on many posts over the internet that union is the right choice for that.
However when I try to publish my float (using sprintf) I get 0,0 instead of 60.8

union test
{

   unsigned char buf[4];
   float number;
}test;

test.buf[0] = 0x42;
test.buf[1] = 0x73;
test.buf[2] = 0x33;
test.buf[3] = 0x34;

PublishBuf(test.buf, 4); //function to publish an array of byte

char StringValue[10];
sprintf(StringValue, "%.2f", test.number );
Particle.publish("Value", StringValue);

test.number is correctly published, I get the right value.
Anybody understand what I’m doing wrong ?

wrong endian-ness

union test
{
  unsigned char buf[4];
  float number;
}test;

void setup() 
{
  Serial.begin(9600);
  test.buf[3] = 0x42;
  test.buf[2] = 0x73;
  test.buf[1] = 0x33;
  test.buf[0] = 0x34;
  Serial.println(test.number);
}

void loop() 
{

}
4 Likes

Thank you very much @BulldogLowell ! It’s working great now

Thanks for the help!

1 Like