Byte Order and Endianness

Carson West

Handling Binary Files

Byte Order and Endianness

This note covers byte order and endianness in Python.

Python, being a high-level language, mostly abstracts away the details of byte order. However, understanding endianness is crucial when working with binary data, network programming, or interacting with lower-level systems.

What is Endianness?

Endianness refers to the order in which bytes of a multi-byte data type (like integers, floats) are stored in computer memory. There are two main types:

Example:

Let’s consider the 32-bit integer 0x12345678 (hexadecimal representation).

Python’s struct Module:

The struct module provides functions to pack and unpack data in different byte orders. You can specify the byte order using format codes:

import struct

# Pack a 32-bit integer in big-endian order
big_endian_data = struct.pack(">I", 0x12345678) 
print(big_endian_data) # Output will vary depending on your system, but the order will be big-endian


# Pack a 32-bit integer in little-endian order
little_endian_data = struct.pack("<I", 0x12345678)
print(little_endian_data) # Output will vary depending on your system, but the order will be little-endian

# Unpack the data (remember to use the same byte order!)
unpacked_big = struct.unpack(">I", big_endian_data)[0]]
unpacked_little = struct.unpack("<I", little_endian_data)[0]]
print(hex(unpacked_big)) # Output: 0x12345678
print(hex(unpacked_little)) # Output: 0x12345678

Python Struct Module (Byte Order in Network Programming)

Determining Your System’s Endianness:

import sys
print(sys.byteorder) # Output: 'little' (on most modern systems)

Important Considerations: