Python Convert String to Hex
Like most programming languages, strings play a crucial role in functioning programs. This is no different in the case of Python.
In Python, a string refers to an immutable sequence of Unicode characters that are represented using the str
data type.
We can create a string by enclosing the characters inside a single ' '
, double " "
, or triple ''' '''
or """ """
quotes.
For example, the following is a basic string in Python:
s = "Hello, World!"
Hex or hexadecimals refer to a base-16 representation of numbers by using the digits 0-9
and the letters a-f
. In Python, any string starting with the prefix 0x
represent a hexadecimal literal.
For example:
num = 0x1a3
In this tutorial, we are going to explore the various methods and techniques that you can use to convert a string type ito hexadecimal in Python.
Method 1 - Using the Hex Method
The first and most basic method that we can use to convert a given string into hex is the hex()
function. The hex method allows us to convert a specified integer into the hexadecimal string representation.
This means, we can use the int(x, base)
function with a 16 as the base to convert the string into an integer.
Function syntax is as shown:
hex(n)
The function takes the required integer as the argument and returns the hexadecimal representation of the input as a string.
NOTE: As you can guess, the function returns the value with the prefix 0x
to denote that the value is in hex form.
The following exampel shows how to use the hex()
method to convert the string into hex.
>>> s = "geekbits"
>>> hex_str = ''.join([hex(ord(c))[2:] for c in s])
>>> print(hex_str)
6765656b62697473
As you can see from the output above, the code above should return the hex representation of the input string. Keep in mind that since the hex function can only convert an int to hex, we need to first convert the string into int using the ord function.
Method 2 - Using the Encode Function
We can also use the encode()
function in Python to convert a string into hex. The encode method works by converting the string into a byte sequence which we cna then pass to the hex()
function as shown.
>>> s = "geekbits".encode('utf-8')
>>> hex_str = s.hex()
>>> print(hex_str)
6765656b62697473
In the example above, we start by calling the encode method to convert the string into bytes. Finally, we convert the resulting value into hex using the hex function.
Method 3 - Using the ast.literal_eval() Method
We can also use the literal_eval()
method to convert a string into hex. We start by converting the string into integer representation and then pass the result to the hex()
function.
Example usage is as shown below:
>>> str = "0xfffa"
>>> str_int = eval(str)
>>> print(hex(str_int))
0xfffa
Conclusion
In this tutorial, you learned how we can use various methods and techniques in Python to convert a given string into hexadecimal. You will notice that all the methods discussed, we use the hex()
method. This is because no matter which method you use to convert the string to int, you will need the hex method to turn it into a hexadecimal.