What Is Big/Little Endian?
Computers perform computations on data that is represented with bytes, i.e., sequences of
1s. A computer program manipulates data by loading data from memory (RAM, Cache, Disk, SSD), performing computations based on that data, and storing the resulting data back into the memory.
A computer program loads and stores bytes in memory.
A data type such as an integer may require multiple “slots” in memory. For example, a 32-bit integer consists of four bytes with 8-bits each. If the storage system stores one byte in one slot, the single integer number requires four slots.
The endian determines the order in which the memory slots map to the data structure.
- Big endian stores the first (most significant) part of the data structure (e.g., integer) at the first memory position.
- Little endian stores the first (most significant) part of the data structure at the last memory position.
Here’s an example where an integer data structure is comprised of four bytes that are stored in memory:
Hex String to Little Endian
Per default, the Python hex string appears in what you may call a “big endian” order, i.e., the most significant hex digit comes first.
📝 Note: This doesn’t mean that Python uses big endian to store data in your computer’s memory—it really depends on your computer architecture and environment. Here we only focus on the representation in Python code.
To convert a big-endian hex string to a little-endian hex string, you can reverse the order of the digits using
bytearray.fromhex(hex_string)[::-1]. The slicing expression
[::-1] inverts the order of hex digits.
Here’s an example:
def hex_to_little_endian(hex_string): little_endian_hex = bytearray.fromhex(hex_string)[::-1] return little_endian_hex print(hex_to_little_endian('aabb')) # bytearray(b'\xbb\xaa')
You can see that the hexadecimal string
'aabb' is converted to the little endian hex string
'bbaa' in bytearray format
\x character just indicates it’s a hex.
Each hex digit consists of two characters such as
Remember that each hex digit from
f represents the decimal
15. To encode 16 different positions in binary, you need four bits
So two hex numbers together represent a sequence of 8 bits, i.e., one byte. The byte array, thus, consists of two bytes represented by two hex numbers per byte.
🌍 Recommended Tutorial: Python Hex String to Big Endian
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.