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.
Right
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
← Previous
Arduino Setting or Clearing a Bit by Variable Position
Next →
Arduino Alternative to delay() Not Working
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro