How do programmers convert between big-endian and little-endian?
General knowledgeAsked by Anonymous from Dallas, United States23 Oct 2026, 11:14 PM ISTUpdated
In one line
Big-endian and little-endian are two ways to store multi-byte data, and converting between them means reversing the order of bytes.
The rule
To convert data between big-endian and little-endian, reverse the order of the bytes.
There is no single formula, but the process is:
For a 4-byte number: [byte1][byte2][byte3][byte4] (big-endian) becomes [byte4][byte3][byte2][byte1] (little-endian).
Let's understand it simply
Imagine you are writing a 4-digit number, like 1234, on four separate slips of paper. In big-endian, you place the slip with '1' first, then '2', then '3', then '4'. In little-endian, you start with '4', then '3', then '2', then '1'. So, the order is flipped.
Think of a cricket scoreboard. If the numbers are shown from left to right (big-endian), the highest place comes first. If you reverse the order (little-endian), the lowest place comes first.
Big-endian vs Little-endian (4-byte number)
Order
Big-endian
Little-endian
Byte order
[byte1][byte2][byte3][byte4]
[byte4][byte3][byte2][byte1]
Example (0x12345678)
12 34 56 78
78 56 34 12
How it works
Take the number you want to convert (for example, a 32-bit integer).
Break it into bytes (each byte is 8 bits).
Write down the bytes in the order they appear (big-endian: most significant byte first; little-endian: least significant byte first).
To convert, simply reverse the order of the bytes.
Put the bytes back together in the new order.
Converting Big-endian to Little-endian
Break number into bytes
Reverse the byte order
Recombine the bytes
The story behind it
The terms "big-endian" and "little-endian" come from a story in the book "Gulliver's Travels" about which end of an egg to break. In computers, different processors (like Intel and ARM) use different ways to store multi-byte numbers. This caused confusion when sharing data, so programmers needed a way to convert between the two formats.
Where you see it in real life
File transfers between computers: If you copy a file from a Windows PC (little-endian) to an old Mac (big-endian), the bytes may need to be swapped.
Network protocols: The Internet usually uses big-endian order (called "network byte order"), so data sent over the network is converted if your computer uses little-endian.
Reading data from sensors: Some sensors send data in big-endian, but your microcontroller might use little-endian, so you must convert.
Programming in C/C++: When you read or write binary files, you may need to swap bytes to match the file's format.
Why it matters
Prevents errors when sharing data between different computers.
Needed for correct results in networking, file reading, and device communication.
Important in exams and interviews for computer science.
Used in programming for embedded systems, networking, and software development.
Worked example
Let's convert the 32-bit hexadecimal number 0x12345678 from big-endian to little-endian: