2 years, 5 months ago.

Difference between union compiled in mbed and arm-none-eabi-gcc?


I have four bytes in a field and I want to make float from that. I use this function:

my function:

float calculate_float(uint8_t val0, uint8_t val1, uint8_t val2, uint8_t val3)
  union u_tag {
    uint8_t b[4];
    float val;
  } u;

  u.b[0] = val0;
  u.b[1] = val1;
  u.b[2] = val2;
  u.b[3] = val3;

  return u.val;

I use it like this:

float X = calculate_float(122,70, 86, 66);

Compiled under online mbed, I have 53.568825 in X. If I compile it under arm-none-eabi-gcc locally, I have 1317317773.000000 in X.

Could somebody please explain me what is happening there :0 ? Thank you a lot in advance!

2 Answers

2 years, 5 months ago.


The online compiler uses ARMCC (the same compiler as Keil). I assume its a difference in the compilers, thought I dont know the specifics.


2 years, 5 months ago.

Check the endianness of both compilers. 0x7A (122), 0x46(70), 0x56(86),0x42(66) = 53.5688248 if using little endian, but 2.57456074e+35 using big endian. Although the big endian option doesn't match your results so there's another factor at work . I used https://www.scadacore.com/tools/programming-calculators/online-hex-converter/ for conversion.