Python Hex String to Little Endian (Bytes/Integer)

4.5/5 - (2 votes)

What Is Big/Little Endian?

Computers perform computations on data that is represented with bytes, i.e., sequences of 0s and 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:

Figure: Big endian stores the most significant byte 0x0A at address a. Little endian stores 0x0A at the last memory address a+3 (source)

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

# bytearray(b'\xbb\xaa')

You can see that the hexadecimal string 'aabb' is converted to the little endian hex string 'bbaa' in bytearray format bytearray(b'\xbb\xaa'). The \x character just indicates it’s a hex.

Each hex digit consists of two characters such as b and b.

Remember that each hex digit from 0 to f represents the decimal 0 to 15. To encode 16 different positions in binary, you need four bits 0000 to 1111.

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