8 Ways to Convert String to Hexadecimal in Python

Do you want to know “how to convert string to hexadecimal in Python?” In this Python post, Let me tell you 8 different methods with practical examples to convert string to hex in Python.

Converting a string to its hexadecimal representation is a common task in Python, especially when dealing with binary data or encoding text for transmission or storage.

In databases or file systems, hexadecimal representation may store binary data or strings in a more compact or readable format. This can be useful for storage efficiency or specific data formats requiring hexadecimal representation.

These are the 8 different methods to convert string to hexadecimal in Python, each with advantages.

  1. Using the hex() method
  2. Using the binascii.hexlify() function
  3. Using list comprehension and the ord() function
  4. Using the encode () method
  5. Using the literal_eval () method
  6. Using the int() function:
  7. using bytes() method
  8. using format() method.

Convert String to Hexadecimal in Python using the hex() method

The hex() method is a built-in function in Python that converts an integer to its hexadecimal representation. We can use this method and the ord() function to convert each string character to its hexadecimal equivalent.

Using the hex() method is a straightforward and concise way to make it a simple choice for converting individual characters to hex.

READ:  For loop vs while loop in Python

Code

string = "Python"
hex_representation = ''.join(hex(ord(char)) for char in string)
print(hex_representation)

In this code, we’ve converted each character to its corresponding ASCII value using the ord() function in Python, then converted each ASCII value to its hexadecimal representation using the hex() method.

Output

0x500x790x740x680x6f0x6e

You can refer to the following screenshot to convert string to hexadecimal in Python using the hex() method.

Convert String to Hexadecimal in Python

How to convert a Python string to Hex format using the hexlify() function

The binascii.hexlify() function from the binascii module converts binary data to ASCII-encoded hexadecimal representation. We can encode the string to binary and then use this function to obtain its hex representation.

Let’s see the example to convert string to hexadecimal in Python using binascii.hexlify() method.

Code

import binascii

string = "Python"
hex_representation = binascii.hexlify(string.encode()).decode()
print(hex_representation)

It first encodes the string “Python” into bytes using .encode(), then converts these bytes to a hexadecimal representation using binascii.hexlify().

binascii.hexlify(string.encode()).decode()

Finally, it decodes the resulting bytes to get a hexadecimal string and prints it.

Output

507974686f6e

Attached is a screenshot of my Python code executed in Visual Studio.

How to convert string to hex in Python

String to hexadecimal in Python Using list comprehension and the ord() function

List comprehension provides a concise way to iterate over characters in a string and apply the ord() function to obtain their ASCII values. Then, we can convert these values to hexadecimal values.

Here is an example of converting a Python string to hexadecimal.

Code

string = "Python"
hex_representation = "".join([hex(ord(char)) for char in string])
print(hex_representation)

In this code, we use list comprehension to iterate over every string character and convert every character to its ASCII value using the ord() method in Python.

Finally, it combines all the hexadecimal representations into a single string and prints it.

READ:  indexerror: string index out of range in Python

Output

0x500x790x740x680x6f0x6e

Here’s a screenshot showcasing my Python code in Visual Studio.

String to Hexadecimal in Python

How to convert a string to hex in Python using the encode() method

The encode() method can convert a string to bytes using a specified encoding, such as UTF-8. We can then obtain the hex representation of these bytes.

Let’s see the example to convert string to hexadecimal in Python using the encode method.

Code

string = "Python"
hex_representation = string.encode('utf-8').hex()
print(hex_representation)

This code utilizes Python’s built-in string encoding and hexadecimal conversion methods.

string.encode('utf-8').hex()

It first encodes the string “Python” into bytes using the UTF-8 encoding with .encode(‘utf-8‘), then converts these bytes directly to a hexadecimal representation using the .hex() method in Python.

Output

507974686f6e

I have included a screenshot displaying my Python code from Visual Studio.

How to convert a Python string to Hex format

Python Convert String to Hex using literal_eval() method

The literal_eval() function from the ast module evaluates a string containing a Python literal or container display. We can utilize this function to convert the string to hexadecimal representation.

Let’s see the example of converting a string to hex in Python.

Code

from ast import literal_eval
string = "0x"+input("Enter the string : ")
converted_string = literal_eval(string)
print(converted_string)
print("type", type(converted_string))
hex_value = hex(converted_string)
print(hex_value)
print(type(hex_value))

Output

Enter the string : abcd
43981
type <class 'int'>
0xabcd
<class 'str'>

Here’s a screenshot of my Python code running in Visual Studio.

How to convert character to hexadecimal in Python

How to convert character to hexadecimal using the int() function

The int() function in Python can convert a string to an integer. By specifying the base argument as 16, we can directly obtain the hexadecimal representation of the string.

Code

string = "Python"
hex_representation = hex(int(string.encode('utf-8').hex(), 16))
print(hex_representation)

Using the int() method to convert string to hexadecimal is a bit more complex because it converts the string “Python” into its integer representation and then converts that integer back to a hexadecimal string.

READ:  Python find max value in a dictionary [4 Methods]

Output

0x507974686f6e

Attached is a screenshot of my Python code in Visual Studio.

How To Convert String To Hexadecimal Number in Python

How To Convert Str To Hexadecimal Number in Python using bytes() method

The bytes() method converts the given string to a bytes object. We can then obtain the hexadecimal representation of these bytes using the hex() method.

Code

string = "Python"
hex_representation = bytes(string, 'utf-8').hex()
print(hex_representation)

It encodes the string “Python” into bytes using UTF-8 encoding with bytes(string, ‘utf-8’), then converts these bytes directly into a hexadecimal representation using the .hex() method.

Output

507974686f6e

Displayed below is a screenshot of my Python code from Visual Studio.

convert ascii string to hex python

Python Str to Hex using format() method

We use the format() method in Python to convert each string character into its corresponding ASCII value using ord(char) and then format it as a hexadecimal string using the format specifier.

Let’s see the example of converting a Python string to hex, including the format() method.

Code

string = "Python"
hex_representation = ''.join(format(ord(char), 'x') for char in string)
print(hex_representation)

This code iterates over each character in the string “Python” and converts every character’s ASCII value into its hexadecimal representation using the format() function with the format specifier ‘x’.

Output

507974686f6e

Presented below is a screenshot of my Python code executed in Visual Studio.

python find hex value in string

Conclusion

In this article, we’ve explored 8 different methods like hex(), encode(), hexlify(), etc., to convert string to hexadecimal in Python. Each method offers its advantages in simplicity, efficiency, or versatility.

By understanding these methods and their benefits, you can choose the most suitable approach for your specific use case, whether it involves encoding text for transmission, processing binary data, or any other scenario.

You may also like to read: