Development

Python Convert String to Hex

In this tutorial, we will explore the various methods and techniques you can use to convert a string type to hexadecimal in Python.
Captain Salem 2 min read
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 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 using the digits 0-9 and the letters a-f. In Python, any string starting with the prefix 0x represents a hexadecimal literal.

For example:

num = 0x1a3

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 converts a specified integer into a 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.

The 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 the value in hex form.

The following example 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. Remember that since the hex function can only convert an int to hex, we need first to convert the string into an 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 encoding method converts the string into a byte sequence, which we can 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 call 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 convert the string into an 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 we use the hex() method for all the methods discussed. No matter which method you use to convert the string to int, you will need the hex method to turn it into a hexadecimal.

Share
Comments
More from GeekBits

Join us at GeekBits

Join our members and get a currated list of awesome articles each month.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to GeekBits.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.