# How to interpret 4 bytes in reverse as float without memcpy?

**URL:** <https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000>\
**Category:** Programming\
**Created:** [July 15, 2024, 10:43pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000 "2024-07-15T22:43:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![felic](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@felic](https://forum.arduino.cc/u/felic)\
**Post date:** [July 15, 2024, 10:44pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/1 "2024-07-15T22:44:00Z")

</div>

I am reverse engineering a proprietary packet based  
protocol and working on a device to intercept it. For that I am receiving the packets one by one, storing them temporarily in a variable and then forwarding them.  
But between storing and forwarding I also want to manipulate these packets and I can't really find an elegant way of doing this.

The most obvious way of course would be to store everything as a uint8\_t array and then read/manipulate the individual bytes. But some of the data in some packets is stored in little endian and needs to be interpreted as float.

For the moment I have created this crazy overly complicates struct:

```arduino
        struct User {
            uint8_t startSequence[2]; // 2 bytes for START SEQUENCE
            uint8_t bodySize; // 1 byte for BODY SIZE
            uint8_t type; // 1 byte for TYPE
            uint8_t unknown; // 1 byte for UNKNOWN
            uint8_t entityId; // 1 byte for ENTITY ID
            uint8_t reserved[3]; // 3 bytes for RESERVED
            std::vector<uint8_t> value; // Variable size for VALUE
            uint8_t endSequence[2]; // 2 bytes for END SEQUENCE

            User(const uint8_t* data, size_t length) {
                if (length < 11) {
                    throw std::invalid_argument("Data too short to be a valid User packet");
                }

                // Parse header
                startSequence[0] = data[0];
                startSequence[1] = data[1];
                bodySize = data[2];
                type = data[3];
                unknown = data[4];

                if (type != Types::USER) {
                    throw std::invalid_argument("Invalid type for User packet");
                }

                // Parse body
                entityId = data[5];
                reserved[0] = data[6]; 
                reserved[1] = data[7];
                reserved[2] = data[8];
                value.assign(data + 9, data + length - 2);

                // Parse footer
                endSequence[0] = data[length - 2];
                endSequence[1] = data[length - 1];

                if (startSequence[0] != START_SEQUENCE[0] || startSequence[1] != START_SEQUENCE[1]) {
                    throw std::invalid_argument("Invalid start sequence");
                }

                if (endSequence[0] != END_SEQUENCE[0] || endSequence[1] != END_SEQUENCE[1]) {
                    throw std::invalid_argument("Invalid end sequence");
                }
            }

            /**
             * This function returns the value of the packet as a boolean.
             */
            bool getBoolValue() const {
                if (value.size() != 1) {
                    throw std::invalid_argument("Value size is not 1");
                }

                return value[0] == 0x01;
            }

            /**
             * This function returns the value of the packet as an unsigned 8-bit integer.
             */
            uint8_t getUint8Value() const {
                if (value.size() != 1) {
                    throw std::invalid_argument("Value size is not 1");
                }

                return value[0];
            }

            /**
             * This function returns the value of the packet as a 32-bit float in little-endian format.
             */
            float getFloatLeValue() const {
                if (value.size() != 4) {
                    throw std::invalid_argument("Value size is not 4");
                }

                // Reorder the bytes to form the correct little-endian format
                uint32_t temp = (value[0]) |
                                (value[1] << 8) |
                                (value[2] << 16) |
                                (value[3] << 24);
                float result;
                memcpy(&result, &temp, sizeof(result));
                return result;
            }
        };

```

And even though this works, I am rsther dissatisfied with it because I'm sure there is a way cleaner way of doing this.

Ideally I would be able to be able to just map my data packet into the struct and be able to do sth like

```arduino
User* packet = reinterpret_cast<User*>(data);
packet->value = 3.518;

```

In a way that the original byte array is correctly updated with the little endian representation for these 4 bytes.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [July 15, 2024, 11:26pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/2 "2024-07-15T23:26:35Z")

</div>

All the Arduino CPUs are natively little-endian, so you should be able to just memcpy() from your packet into your float.  
Theoretically, the compiler is smart enough to optimize such memcpy use...

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [July 16, 2024, 9:14am UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/3 "2024-07-16T09:14:32Z")

</div>

> [@felic](#):
>
> ```arduino
> User* packet = reinterpret_cast<User*>(data);
> packet->value = 3.518;
> 
> ```

This invokes [Undefined Behavior](https://en.cppreference.com/w/cpp/language/ub) and breaks the optimizer's TBAA.

See [Don't use unions or pointer casts for type punning](https://tttapa.github.io/Pages/Programming/Cpp/Practices/type-punning.html) for alternatives.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 16, 2024, 9:55am UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/4 "2024-07-16T09:55:42Z")

</div>

Please, explain your objectives with the help of a binary32 formatted 4-byte data.

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [July 16, 2024, 10:12am UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/5 "2024-07-16T10:12:54Z")

</div>

> [@PieterP](#):
>
> This invokes [Undefined Behavior](https://en.cppreference.com/w/cpp/language/ub) and breaks the optimizer's TBAA.

Is there a type-accessibility violation here if both the raw data and the struct are a bunch of `uint8_t` (separately because they're both a bunch of the same stuff, and that stuff is a `typedef` for `unsigned char`)?

However, for that same reason, you can't avoid the `memcpy`

- it's needed as "barrier" to do the aforementioned [_type punning_](https://tttapa.github.io/Pages/Programming/Cpp/Practices/type-punning.html) without Undefined Behavior
- the `value` offset in the struct is not 32-bit-aligned, so it may not be valid to access it directly as a **`float`**
- on the plus side, the mechanics of the copy will be as optimized as possible for the platform

I got a direct cast to work (apparently)

```cpp
#include <stdexcept>

// struct UserBase {
// virtual bool boolValue() const = 0;
// virtual uint8_t uint8Value() const = 0;
// virtual float floatValue() const = 0;
// };

template<typename T>
struct User { //: UserBase {
  uint8_t startSequence[2]; // 2 bytes for START SEQUENCE
  uint8_t bodySize; // 1 byte for BODY SIZE
  uint8_t type; // 1 byte for TYPE
  uint8_t unknown; // 1 byte for UNKNOWN
  uint8_t entityId; // 1 byte for ENTITY ID
  uint8_t reserved[3]; // 3 bytes for RESERVED
  uint8_t value[sizeof(T)]; // Variable size for VALUE -- `T value` would not be 32-bit-aligned
  uint8_t endSequence[2]; // 2 bytes for END SEQUENCE

  bool boolValue() const {
    return uint8Value(); // or does it have to be 0x01 exactly?
  }
  uint8_t uint8Value() const {
    if (sizeof(value) != 1) {
      throw std::invalid_argument("Value size is not 1");
    }

    return value[0];  
  }
  float floatValue() const {
    if (sizeof(value) != 4) {
      throw std::invalid_argument("Value size is not 4");
    }

    float result;
    memcpy(&result, value, sizeof(result));
    return result;
  }
};

void setup() {
  Serial.begin(115200);
  uint8_t data1[] = {
    0, 1,
    2, 3, 4, 5,
    6, 7, 8,
    9,
    10, 11,
  };
  auto *packet1 = reinterpret_cast<User<uint8_t>*>(data1);
  uint8_t data4[] = {
    0, 1,
    2, 3, 4, 5,
    6, 7, 8,
    0x44, 0x33, 0x22, 0xc1,
    13, 14,
  };
  auto *packet4 = reinterpret_cast<User<float>*>(data4);
  Serial.println(packet1->boolValue());
  Serial.println(packet1->uint8Value());
  Serial.println(packet4->floatValue());
}

void loop() {
  delay(10);
}

```

Because the data is variable-size in the middle, you need distinct types, especially if you want to cast it back to raw data. The decision for the exact type, previously based on length, is done manually in the demo.

Note there is a commented-out `UserBase`, in an attempt to handle the results polymorphically. The `reinterpret_cast` does not fail (it can't), but the function calls don't work, probably because with **`virtual`** methods, the class instances have a reference to their _vtable_, and it's not in the raw data.

If you want to handle these generically, you'll need to add something, depending on their usage.

---

<div class="post-metadata">

**Author:** ![felic](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@felic](https://forum.arduino.cc/u/felic)\
**Post date:** [July 16, 2024, 10:58am UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/6 "2024-07-16T10:58:03Z")

</div>

@kenb4 I see, thanks for the explanation.  
@westfw Does this apply to the ESP32 as well?

Would it help if I went for a struct with fixed value size instead?

I tried it like this:

```arduino
#include <Arduino.h>

typedef struct {
    uint8_t start_seq[2];
    uint8_t body_size;
    uint8_t type;
    uint8_t unknown;
    uint8_t entity_id;
    uint8_t reserved[3];
    float value;
    uint8_t end_seq[2];
} Packet;

void printRawPacket(const char* message, const uint8_t* packet, size_t length) {
    Serial.print(message);
    for (int i = 0; i < length; i++) {
        Serial.printf("%02X ", packet[i]);
    }
    Serial.println();
}

// Packet interceptor function
void handlePacket(uint8_t* raw_packet, size_t length) {
    // Create a pointer to the packet structure
    Packet* packet = (Packet*)raw_packet;

    printRawPacket("Original bytes: ", raw_packet, sizeof(raw_packet));
    // Correctly Prints: "Original bytes: BE EF 08 03 00 09 00 00 00 00 00 C0 40 EF BE"

    // Access the float value without creating a copy of the 4 bytes
    Serial.print("Original value: ");
    Serial.println(packet->value);
    // Incorrectly prints a random value or "ovf" instead of 6.00

    // Modify the float value without creating a copy of the 4 bytes
    packet->value = -120.0f; // little endian would be 00 00 F0 C2; big endian would be C2 F0 00 00
    Serial.print("Modified value: ");
    Serial.println(packet->value); // Prints -120.0 correctly

    printRawPacket("Modified bytes: ", raw_packet, sizeof(raw_packet));
    // Incorrectly Prints: "Modified bytes: BE EF 08 03 00 09 00 00 00 00 00 C0 00 00 F0"
    // Showing the float value was not stored correctly and the footer was overwritten
    // Expected output would have been: "Modified bytes: BE EF 08 03 00 09 00 00 00 00 00 00 00 F0 C2 EF BE"
}

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

    // Example packet
    uint8_t raw_packet[] = {
        // ------ HEADER ------
        0xBE, 0xEF, // start sequence
        0x08, // body size uint8_t
        0x03, // type uint8_t
        0x00, // unknown
        // ------ HEADER ------

        // ------ BODY ------
        0x09, // entity id uint8_t
        0x00, 0x00, 0x00, // reserved
        0x00, 0x00, 0xC0, 0x40, // value (float, little-endian) // 6.0f
        // ------ BODY ------

        // ------ FOOTER ------
        0xEF, 0xBE // end sequence
        // ------ FOOTER ------
    };

   handlePacket(raw_packet, sizeof(raw_packet));
}

void loop() {
    // Nothing to do here
}

```

But as mentioned in the comments, it's not working right.

The only way I was able to get it to work correctly, was by introducing a getter and setter again:

```arduino
typedef struct {
    uint8_t start_seq[2];
    uint8_t body_size;
    uint8_t type;
    uint8_t unknown;
    uint8_t entity_id;
    uint8_t reserved[3];
    uint8_t value[4];
    uint8_t end_seq[2];

    // Function to get the float value from the raw packet in little-endian format
    float getFloatLeValue() const {
        // reverse byte order and return as float
        uint32_t temp = (value[3] << 24) | (value[2] << 16) | (value[1] << 8) | value[0];
        float result;
        memcpy(&result, &temp, sizeof(result)); // Copy uint32_t to float
        return result;
    }

    // Function to set the float value into the raw packet in little-endian format
    void setFloatLeValue(float newValue) {
        // Copy float to uint32_t
        uint32_t temp;
        memcpy(&temp, &newValue, sizeof(temp));
        // Copy uint32_t to value in little-endian format
        value[0] = temp & 0xFF;
        value[1] = (temp >> 8) & 0xFF;
        value[2] = (temp >> 16) & 0xFF;
        value[3] = (temp >> 24) & 0xFF;
    }
} Packet;

```

But this just seems unnecessarily complex and complicated when all I wanna do is interpret 4 bytes in reverse.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [July 16, 2024, 12:21pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/7 "2024-07-16T12:21:49Z")

</div>

> [@felic](#):
>
> ```arduino
> void handlePacket(uint8_t* raw_packet, size_t length) {
> // Create a pointer to the packet structure
> Packet* packet = (Packet*)raw_packet;
> 
> ```

As mentioned by @PieterP, that cast is illegal and may cause undefined behavior.

Also, on an ESP32 your `Packet` type and `raw_packet` array are different sizes due to alignment by the compiler.

BTW, `typedef` is sooo 1980's 'C' language. Don't need it to define a `struct` type in `C++`.

Try this:

```arduino
#include <Arduino.h>

struct __attribute__ ((packed)) Packet {
  uint8_t start_seq[2];
  uint8_t body_size;
  uint8_t type;
  uint8_t unknown;
  uint8_t entity_id;
  uint8_t reserved[3];
  float value;
  uint8_t end_seq[2];
};

void printRawPacket(const char* message, Packet* packet) {
  Serial.print(message);
  uint8_t *rawBytes = reinterpret_cast<uint8_t *>(packet);
  for (int i = 0; i < sizeof(Packet); i++) {
    Serial.printf("%02X ", rawBytes[i]);
  }
  Serial.println();
}

// Packet interceptor function
void handlePacket(Packet* packet) {

  printRawPacket("Original bytes: ", packet);

  Serial.print("Original value: ");
  Serial.println(packet->value);

  // Modify the float value without creating a copy of the 4 bytes
  packet->value = -120.0f;
  Serial.print("Modified value: ");
  Serial.println(packet->value);

  printRawPacket("Modified bytes: ", packet);
}

void setup() {
  Serial.begin(115200);
  delay(2000);

  Packet tempPacket;
  // Example packet
  uint8_t raw_packet[] = {
    // ------ HEADER ------
    0xBE, 0xEF, // start sequence
    0x08, // body size uint8_t
    0x03, // type uint8_t
    0x00, // unknown
    // ------ HEADER ------

    // ------ BODY ------
    0x09, // entity id uint8_t
    0x00, 0x00, 0x00, // reserved
    0x00, 0x00, 0xC0, 0x40, // value (float, little-endian) // 6.0f
    // ------ BODY ------

    // ------ FOOTER ------
    0xEF, 0xBE // end sequence
    // ------ FOOTER ------
  };

  Serial.println(sizeof(raw_packet));
  Serial.println(sizeof(Packet));
  memcpy(&tempPacket, raw_packet, sizeof(Packet));
  handlePacket(&tempPacket);
}

void loop() {
}

```

**Result:**

```arduino
15
15
Original bytes: BE EF 08 03 00 09 00 00 00 00 00 C0 40 EF BE 
Original value: 6.00
Modified value: -120.00
Modified bytes: BE EF 08 03 00 09 00 00 00 00 00 F0 C2 EF BE 

```

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [July 16, 2024, 12:24pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/8 "2024-07-16T12:24:35Z")

</div>

> [@felic](#):
>
> `printRawPacket("Original bytes: ", raw_packet, sizeof(raw_packet));`

You cannot use sizeof to get the number of elements in an array if all you have is a pointer to it.

> [@felic](#):
>
> `Packet* packet = (Packet*)raw_packet;`

This is equivalent to the `reinterpret_cast` you had before, it also invokes Undefined Behavior. See also: [Don't use C-style casts](https://tttapa.github.io/Pages/Programming/Cpp/Practices/c-style-casts.html)

---

<div class="post-metadata">

**Author:** ![felic](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@felic](https://forum.arduino.cc/u/felic)\
**Post date:** [July 16, 2024, 12:48pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/9 "2024-07-16T12:48:33Z")

</div>

@gfvalvo Thank you, to my surprise, this actually works. So I guess float on ESP32 has always been little-endian after all? Also thanks for showing me the modern way of doing it.

@PieterP Thanks for clearing that up. I was genuinely not aware of that.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [July 16, 2024, 12:56pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/10 "2024-07-16T12:56:05Z")

</div>

> [@felic](#):
>
> @PieterP Thanks for clearing that up. I was genuinely not aware of that.

BTW, this:

> [@gfvalvo](#):
>
> ` uint8_t *rawBytes = reinterpret_cast<uint8_t *>(packet);`

is one of the few cases where you're allowed to type pun via a pointer. You can cast to a `uint8_t *` in order to access the individual bytes in the original data structure.

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [July 16, 2024, 2:11pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/11 "2024-07-16T14:11:04Z")

</div>

> [@felic](#):
>
> float on ESP32 has always been little-endian after all?

Usually, _float_ stands for "Floating Point Number" like 0.15625 (for example). In ESP32, a floating point number can be stored as --

**1.** 4-byte (single precision) binary32/IEE-754 formatted data (Fig-2.1, little endian) by declaring:

```arduino
float myNum = 0.15625;

```

 ![IEEE754Float](https://europe1.discourse-cdn.com/arduino/original/4X/4/d/8/4d8c60256a3280d2f24149f5885fab3d3f619922.png)  
Figure-2.1:

**2.** 8-byte (double precision) binary64/EE-754 formatted data (Fig-2.2, little endian) by declaring:

```arduino
double myNum = 1.23456;

```

 ![binary64](https://europe1.discourse-cdn.com/arduino/original/4X/e/1/f/e1f3e8b74e1e5382dbb973795803c163476f4f7d.png)  
Figure-2.2:

---

<div class="post-metadata">

**Author:** ![felic](https://avatars.discourse-cdn.com/v4/letter/f/d07c76/32.png) [@felic](https://forum.arduino.cc/u/felic)\
**Post date:** [July 16, 2024, 6:05pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/12 "2024-07-16T18:05:24Z")

</div>

Do you have any suggestions on how to design several different packet types without too much redundancy while keeping the order intact?

For example I would like to do something among the lines of:

```arduino
struct __attribute__ ((packed)) GenericPacket {
    uint8_t start_seq[2]; // always {0xBE, 0xEF}
    uint8_t body_size;
    uint8_t packet_type;
    uint8_t header_byte;
    uint8_t body[body_size];
    uint8_t end_seq[2]; // always {0xEF, 0xBE}
};

struct __attribute__ ((packed)) GenericEntityPacket : public GenericPacket {
    // Replace uint8_t body[body_size]; with:
        uint8_t entity_id;
        uint8_t reserved[3];
        uint8_t remaining_body[body_size-4];
};

// Example: BE EF 08 03 00 58 00 00 00 00 00 C0 40 EF BE
struct __attribute__ ((packed)) FloatEntityPacket : public GenericEntityPacket {
    // Replace `uint8_t remaining_body[body_size-4];` with
        float value;
};

// Example: BE EF 05 03 00 59 00 00 01 EF BE
struct __attribute__ ((packed)) BoolEntityPacket : public GenericEntityPacket {
    // Replace `uint8_t value[body_size-4];` with
        uint8 unused[3];
        bool value;
};

// Example: BE EF 05 03 00 5C 00 00 00 04 EF BE
struct __attribute__ ((packed)) Uint8tEntityPacket : public GenericEntityPacket {
    // Replace `uint8_t value[body_size-4];` with
        uint8 unused[3];
        uint8_t value;
};

// Example: BE EF 01 18 01 02 EF BE
struct __attribute__ ((packed)) ToggleTunerPacket : public GenericPacket {
    // Replace `uint8_t header_byte;` with:    
        uint8_t state;
    // Replace `uint8_t body[body_size];` with
        uint8_t channel;
};

// Example: BE EF 00 1E 02 EF BE
struct __attribute__ ((packed)) SdRecordPacket : public GenericPacket {
    // Replace `uint8_t header_byte;` with:    
        uint8_t state;
    // Remove `uint8_t body[body_size];`
};

// Example: BE EF 01 1A 01 04 EF BE
struct __attribute__ ((packed)) SnapshotPacket : public GenericPacket {
    // Replace `uint8_t header_byte;` with:    
        uint8_t action;
    // Replace `uint8_t body[body_size];` with
        uint8_t slot;
};

```

Edit: Moved to [How to elegantly inherit from struct overriding/adding fields](https://forum.arduino.cc/t/how-to-elegantly-inherit-from-struct-overriding-adding-fields/1282346)

---

<div class="post-metadata">

**Author:** ![MartinL](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/martinl/32/216911_2.png) [@MartinL](https://forum.arduino.cc/u/MartinL)\
**Post date:** [July 16, 2024, 6:21pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/13 "2024-07-16T18:21:28Z")

</div>

Hi @felic

One way to represent 4-bytes as a floating point number without _memcpy_ is to use a union:

```arduino
// Test: set float to the smallest number greater than 1
union
{
  uint8_t byteData[4]; // byte array
  float f; // float
  struct
  {
    uint32_t mantissa : 23; // 23 bit mantissa
    uint32_t exponent : 8; // 8 bit exponent
    uint32_t sign : 1; // 1 bit sign
  } part;
} singleFloat;

void setup()
{
  SerialUSB.begin(115200); // Start communication on the native USB port
  while(!SerialUSB); // Wait for the console to open 
  singleFloat.part.mantissa = 0x1; // Load mantissa
  singleFloat.part.exponent = 0x7F; // Load exponent 
  singleFloat.part.sign = 0x0; // Load sign 
  for (uint8_t i = 0; i < 4; i++)
  {
    SerialUSB.print(F("Byte"));
    SerialUSB.print(i);
    SerialUSB.print(F(": "));
    SerialUSB.println(singleFloat.byteData[i], HEX);
  }
  SerialUSB.print(F("Float: "));
  SerialUSB.println(singleFloat.f, 8); // Should output: 1.00000012
}

void loop() {}

```

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [July 16, 2024, 6:24pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/14 "2024-07-16T18:24:36Z")

</div>

> [@MartinL](#):
>
> One way to represent 4-bytes as a floating point number without _memcpy_ is to use a union:

This is not allowed either. See the link posted earlier: [Don't use unions or pointer casts for type punning](https://tttapa.github.io/Pages/Programming/Cpp/Practices/type-punning.html)

---

<div class="post-metadata">

**Author:** ![MartinL](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/martinl/32/216911_2.png) [@MartinL](https://forum.arduino.cc/u/MartinL)\
**Post date:** [July 16, 2024, 6:49pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/15 "2024-07-16T18:49:27Z")

</div>

Hi @PieterP

Thanks for the link to the information about type punning.

Looks like my suggestion fell foul of the C++ type system.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [July 16, 2024, 7:36pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/16 "2024-07-16T19:36:42Z")

</div>

> [@felic](#):
>
> I guess float on ESP32 has always been little-endian after all?

Yes. There hasn't been a big-endian CPU released on a long time.  
(Some of the larger ARM, MIPS, and PPC processors have an OPTION (usually selectable at boot time) to run either little-endian or big-endian, but all of the microcontrollers I've seen have been little-endian.  
(I guess there are some Motorola/Freescale/NXP "CPU32" (~68k-like) chips that might qualify as microcontrollers, and are still big-endian. They're not seen very often, though.)

The banning of assorted types of type-punning without an "adequate" replacement is something I find annoying. Those were widely-used techniques in networking and embedded code that AFAIK worked fine for decades.  
"Just use memcpy() and the compiler will optimize it, maybe" is a poor substitute.

---

<div class="post-metadata">

**Author:** ![PieterP](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@PieterP](https://forum.arduino.cc/u/PieterP)\
**Post date:** [July 16, 2024, 7:55pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/17 "2024-07-16T19:55:52Z")

</div>

> [@westfw](#):
>
> The banning of assorted types of type-punning without an "adequate" replacement is something I find annoying. Those were widely-used techniques in networking and embedded code that AFAIK worked fine for decades.  
> "Just use memcpy() and the compiler will optimize it, maybe" is a poor substitute.

I agree. It makes sense from a compiler developer's perspective, but it has some serious downsides for networking and deserialization code.

C++ now finally has [`std::start_lifetime_as`](https://en.cppreference.com/w/cpp/memory/start_lifetime_as), but there's not a single compiler that supports it yet. Meanwhile, most other C++23 features are supported by at least some of the major compilers, so either this is a low-priority feature, or it's hard to implement. Probably both. [Compiler support for C++23 - cppreference.com](https://en.cppreference.com/w/cpp/compiler_support/23)

---

<div class="post-metadata">

**Author:** ![kenb4](https://avatars.discourse-cdn.com/v4/letter/k/bb73d2/32.png) [@kenb4](https://forum.arduino.cc/u/kenb4)\
**Post date:** [July 16, 2024, 9:23pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/18 "2024-07-16T21:23:21Z")

</div>

> [@felic](#):
>
> So I guess float on ESP32 has always been little-endian after all?

The endian-ness of floating point numbers is the same as (multi-byte) integers for a given CPU. If it helps to remember: the sign bit is in the same place, at the high bit.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [July 16, 2024, 11:50pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/19 "2024-07-16T23:50:52Z")

</div>

Intel's C compiler has a bi-endian data feature.  
Declaring a variable as "big endian" means that the compiler will insert a BSWAP instruction after each memory fetch. Since BSWAP is essentially a single-cycle instruction, it's much quicker than any memory fetch (even one from cache), and has negligible impact on performance.

It's rather neat. In theory, it could be added to other compilers. ARM also has a BSWAP-like instruction. (more performance impact on slower CPUs with more rightly coupled RAM, of course.)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 12, 2025, 11:51pm UTC](https://forum.arduino.cc/t/how-to-interpret-4-bytes-in-reverse-as-float-without-memcpy/1282000/20 "2025-01-12T23:51:45Z")

</div>

Closed on January 12, 2025, 11:51pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
