5 Best Ways to Convert Python Bytes to a Hex List

πŸ’‘ Problem Formulation: This article addresses the challenge of converting a sequence of bytes, which are commonly used for binary data storage, into a human-readable list of hexadecimal strings in Python. For example, the input b'\x00\xFF' should yield the output ['00', 'ff'].

Method 1: Using a for-loop and format()

Transforming bytes to a hexadecimal list can be done by iterating over each byte and using the format() function to convert the byte into its corresponding two-digit hexadecimal string. This method is explicit and great for educational purposes, but may not be the most efficient for large data.

Here’s an example:

bytes_data = b'\x00\xFF\xA5'
hex_list = [format(byte, '02x') for byte in bytes_data]
print(hex_list)

Output:

['00', 'ff', 'a5']

This code snippet iterates over the byte string, converting each byte into a hexadecimal string padded with zeroes to ensure two digits, and collects them into a list.

Method 2: Using map() and format()

Python’s map function can be utilised to apply the format() function to each byte in the bytes object. This functional approach is both concise and pythonic, suitable for those familiar with functional paradigms.

Here’s an example:

bytes_data = b'\xDE\xAD\xBE\xEF'
hex_list = list(map(lambda b: format(b, '02x'), bytes_data))
print(hex_list)

Output:

['de', 'ad', 'be', 'ef']

The code uses the map function to apply a lambda that formats each byte to a hexadecimal string. The result is then converted back to a list.

Method 3: Using bytearray and format()

A bytearray is a mutable sequence of integers in the range 0 <= x < 256. You can directly create a bytearray from bytes and proceed with conversion using a similar iteration and formatting strategy.

Here’s an example:

bytes_data = b'\x50\x59\x54\x48'
byte_array = bytearray(bytes_data)
hex_list = [format(b, '02x') for b in byte_array]
print(hex_list)

Output:

['50', '59', '54', '48']

This code snippet demonstrates that a byte can be converted to bytearray. Iteration and formatting are then performed similarly to Method 1 to get each hexadecimal string.

Method 4: Using binascii.hexlify()

The binascii.hexlify() function from the binascii module is a very direct way to convert bytes to a hexadecimal representation. Then, slicing can be used to create the hex list. This method is highly efficient and concise, making it ideal for most practical applications.

Here’s an example:

import binascii
bytes_data = b'\xB0\xB1\xB2'
hex_str = binascii.hexlify(bytes_data).decode('ascii')
hex_list = [hex_str[i:i+2] for i in range(0, len(hex_str), 2)]
print(hex_list)

Output:

['b0', 'b1', 'b2']

This snippet uses the binascii.hexlify() function to create a string of hexadecimal digits then slices it into a list of two-character strings.

Bonus One-Liner Method 5: Using bytes.hex() and a list comprehension

Python 3.5+ introduced a hex() method directly on the bytes object. This straightforward one-liner can quickly become a ‘go-to’ for Python 3.5+ users.

Here’s an example:

bytes_data = b'\xCA\xFE\xBA\xBE'
hex_list = [bytes_data.hex()[i:i+2] for i in range(0, len(bytes_data.hex()), 2)]
print(hex_list)

Output:

['ca', 'fe', 'ba', 'be']

With a single line, this code uses the bytes.hex() method to convert the byte sequence to a string and then slices it into the required format.

Summary/Discussion

  • Method 1: For-loop and format. Clear and educational. Less efficient for large byte strings.
  • Method 2: map() and format. Pythonic and concise. Requires understanding of functional programming concepts.
  • Method 3: Bytearray and format. Shows usage of bytearray concept. Similar performance to using a byte object.
  • Method 4: binascii.hexlify(). Most efficient and concise. Requires an additional import.
  • Method 5: bytes.hex() one-liner. Extremely straightforward. Only available in Python 3.5+.