Python Hex String to Big Endian (Bytes/Integer)

5/5 - (1 vote)

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 hex string to a big endian hex string, simply use the standard bytearray.fromhex(hex_string). The result already is Big Endian.

Here’s an example:

def hex_to_big_endian(hex_string):
    return bytearray.fromhex(hex_string)


print(hex_to_big_endian('aabb'))
# bytearray(b'\xaa\xbb')

You can see that the hexadecimal string 'aabb' is converted to the big endian hex string 'aabb' in bytearray format bytearray(b'\xaa\xbb'). 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 Little Endian