Get hex representation without 0x in Python

avatar

Borislav Hadzhiev

Last updated: Sep 5, 2022

banner

Photo from Unsplash

Get hex representation without 0x in Python #

Use a formatted string literal to get the hex representation of a value without 0x, e.g. result = f'{my_int:x}'. The formatted string literal will format the value in hex without the 0x prefix.

main.py
my_int = 2468 # ✅ Get hex representation of integer without 0x result = f'{my_int:x}' print(result) # 👉️ 9a4 # ------------------------------------------ # ✅ Fixed length of 2 digits with zero left-filling if necessary my_int = 12 result = f'{my_int:02x}' print(result) # 👉️ 0c # ------------------------------------------ # ✅ Convert list of integers to hexadecimal string without 0x my_list = [7, 11, 49, 75, 188, 111] result = ''.join(f'{num:02x}' for num in my_list) print(result) # 👉️ 070b314bbc6f # ------------------------------------------ # ✅ Convert string to hex without 0x my_str = 'hello' result = ' '.join(f'{ord(char):x}' for char in my_str) print(result) # 👉️ 68 65 6c 6c 6f # ------------------------------------------ # ✅ Convert bytes to hex without 0x my_bytes = b'\x61\x70\x6c\x65' result = my_bytes.hex() print(result) # 👉️ 61706c65

The first example uses a formatted string literal to get the hex representation of an integer without the 0x prefix.

main.py
my_int = 2468 result = f'{my_int:x}' print(result) # 👉️ 9a4

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

The x character after the colon stands for hex format. It outputs the number before the colon in base 16, using lowercase letters for the digits above 9.

You can use the same approach if you need to get the hex representation of the integer with a fixed length of N.

main.py
my_int = 12 result = f'{my_int:02x}' print(result) # 👉️ 0c

The :02x syntax is used to pad each value with leading zeros to a fixed width of 2.

You can use a generator expression if you need to convert a list of integers to a hexadecimal string without the 0x prefix.

main.py
my_list = [7, 11, 49, 75, 188, 111] result = ''.join(f'{num:02x}' for num in my_list) print(result) # 👉️ 070b314bbc6f
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use a formatted string literal to convert the current integer to hex without the 0x prefix.

You can use the same approach to convert a string to hex without the 0x prefix.

main.py
my_str = 'hello' result = ' '.join(f'{ord(char):x}' for char in my_str) print(result) # 👉️ 68 65 6c 6c 6f

On each iteration, we pass the current character to the ord() function to get the corresponding Unicode code point.

main.py
print(ord('a')) # 👉️ 97 print(ord('b')) # 👉️ 98

The ord function takes a string that represents 1 Unicode character and returns an integer representing the Unicode code point of the given character.

We joined the results with a space separator, but you can use any other value.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

If you need to convert a bytes object to hex, use the bytes.hex() method.

main.py
my_bytes = b'\x61\x70\x6c\x65' result = my_bytes.hex() print(result) # 👉️ 61706c65

The bytes.hex method returns a string that contains two hexadecimal digits for each byte.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.