In Python and the world of software development, a hexadecimal value (or hex value) is a representation of a number using base-16 digits and alphabets. It consists of digits 0-9 and letters A-F. The prefix
0x is optional and sometimes used to indicate that a value is hexadecimal, but it is not required. Hexadecimal values are used for memory addresses, bitwise operations, data encoding, and representing colors in computer graphics, etc.
This succinct and straight-to-the-point article will walk you through several different ways to turn a string into a hexadecimal value in Python. Without any further ado, let’s get started!
Using the hex() method
If you are using Python 3.5 or newer, you can use the
encode() and the
hex() methods to convert a given string to a hex value with a single line of code.
text = "Sling Academy" hex = text.encode("utf-8").hex() print(hex)
The syntax is very concise and intuitive. I guess you will like it the most. There is one more thing that I think might help you when using the
hex() function. If you want the resulting hex string to be more readable, you can set a custom separator with the
sep parameter as shown below:
text = "Sling Academy" hex = text.encode("utf-8").hex(sep="_") print(hex)
Using the binascii.hexlify() function
This approach works with both old versions of Python (2.x) and new versions of the programming language (3.x). What we will do are:
- Encode the string to bytes using the desired encoding, such as
- Call the
binascii.hexlify()function to convert the bytes to a hexadecimal representation.
- Decode the resulting bytes as a string to obtain the hex value.
import binascii string = "Sling Academy" encoded_bytes = string.encode('utf-8') hex_value = binascii.hexlify(encoded_bytes).decode('utf-8') print(hex_value)
This approach is straightforward, works will all versions of Python and doesn’t rely on any third-party library. However, it requires encoding and decoding steps limited to ASCII characters and might not handle non-ASCII characters well.
Using list comprehension and the ord() function
In this technique, we will convert each character in the string to its corresponding Unicode code point and format it as a hexadecimal value. The steps are:
- Iterate through each character in the string.
ord()to obtain the Unicode code point of each character.
- Format the code point as a hexadecimal string by using the
- Join the hexadecimal strings together with the
A code example is worth more than a thousand words:
string = "Sling Academy" hex_value = ''.join([format(ord(char), 'x') for char in string]) print(hex_value)
Because we use the same input as other examples in this article, the output won’t be a new thing:
The advantage of this technique is that can handle non-ASCII characters and is flexible and customizable. The trade-off is that it requires iterating through each character, which may be less efficient for extremely large strings.