Skip to content

Arduino Byte Order (Endianness) Causes Data Corruption

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about Arduino Byte Order (Endianness) Causes Data Corruption. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Multi-byte values sent over serial or I2C appear corrupted due to endianness mismatch.

Quick Fix

Wrong

uint16_t val = 0x1234;
Serial.write((byte*)&val, 2);  // Sends 0x34, 0x12 (little-endian)
Receiver gets 0x3412 instead of 0x1234.
uint16_t val = 0x1234;
uint8_t buf[2];
buf[0] = (val >> 8) & 0xFF;  // Big-endian: high byte first
buf[1] = val & 0xFF;
Serial.write(buf, 2);

Serial.print("Sent: 0x");
Serial.print(buf[0], HEX);
Serial.println(buf[1], HEX);
Sent: 0x1234 (big-endian).

Prevention

AVR is little-endian (LSB first). Most network protocols use big-endian (MSB first). Convert using manual byte ordering or use word()/makeWord() utilities. For 32-bit values, use uint32_t and shift operators. ESP32 is also little-endian.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### What is endianness?

Endianness defines byte order: little-endian stores the least significant byte at the lowest address; big-endian stores the most significant byte first.

Does endianness affect flash memory?

Yes. If you write multi-byte values to EEPROM or SD card and read on a different system, you must account for endianness.

How do I convert endianness on Arduino?

Swap bytes manually, or use __builtin_bswap16() / __builtin_bswap32() on GCC (supports both AVR and ESP32).

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro