ABC of Electronics cosycom.com
EMBEDDED SYSTEMS & MICROCONTROLLERS · PAGE 66

Endianness and Data Formats

Which byte comes first?

A 16-bit number takes two bytes. Little-endian systems store the low byte first; big-endian systems store the high byte first. Neither is wrong, but both ends of a link must agree.

When sending numbers between devices, build them from bytes with shifts rather than copying raw memory. This works on any chip.

Text formats like decimal strings are easier to debug; binary formats are smaller and faster.

EXAMPLE
uint16_t read_be16(const uint8_t *p) { return ((uint16_t)p[0] << 8) | p[1]; }
uint16_t read_le16(const uint8_t *p) { return ((uint16_t)p[1] << 8) | p[0]; }