This post describes Endianness.
Endian refers to the order of bytes when data consisting of multiple bytes (e.g., 16-bit integers, 32-bit integers, etc.) is stored and transmitted in memory or communication data. This is an important concept because endian differences can cause data to be interpreted incorrectly when exchanging data between computers and communication devices.
There are two main types of endianness:
Big-Endian
In big-endian, the most significant byte (the most important byte) is placed at the first address. This is close to the sequence of numbers normally read by humans (left to right) and is highly readable, so this method is considered the standard for network communications (e.g., TCP/IP protocol).
Example:
If the hexadecimal value 0x01234567 is stored in memory in big-endian,
01 23 45 67 (low address → high address).
Little-Endian
In little endian, the least significant byte (the least important byte) is placed at the first address. This method is characterized by its high processing efficiency in numerical operations, especially in operations such as addition and subtraction, which can be performed at high speeds.For this reason, little-endian is used in Intel-based CPUs (x86, x64, etc.).
Example:
If the hexadecimal value 0x01234567 is stored in memory in little endian,
67 45 23 01 in that order.

Attention
In the following cases, data will be misinterpreted if the conversion process is not performed correctly and must be addressed.
Case1: When sending and receiving data in byte units in communication
Communication protocols often have a strict order in which data is transmitted, typically in big-endian (network byte order). However, if the sender or receiver is operating on a little-endian system, the order of bytes will be reversed if transmitted as is, and the data will not be interpreted correctly.
Points to note:
When sending, the CPU byte order must be converted (endian conversion) to match the communication specifications; functions such as htons() and htonl() (host to network short/long) are available in C and other languages.
Case2: When manipulating a little-endian short or long type with a pointer variable of type char
In C programming, for example, multi-byte data such as short and long may be manipulated one byte at a time with a char* type pointer. In little-endian programming, the lower byte comes first, so if the data is not processed in the expected byte order, it may cause malfunctions.
Points to note:
When accessing with char*
, it is necessary to be aware of byte order.
For example, if you want to extract only the upper byte from 2-byte data of type short, the byte position to be referenced is different for big-endian and little-endian data.
Case3: When porting a program to a different endian system
If a program is ported from a little-endian system (e.g., x86) to a big-endian system (e.g., some embedded CPUs), the following effects may occur.
- Interpretation of byte strings in structures changes (especially at communication and file I/O boundaries).
- Binary files are read/written incorrectly (due to different stored byte order).
- Inconsistent network communication (when hardware does not automatically handle byte order).
Countermeasure Points:
- Explicitly define and convert byte order (using shift operations and macros).
- Use fixed-size types such as uint8_t, uint16_t, etc. to write portable code and manually control byte order.