How to convert a Python string to Hex format?

Print strings to hexadecimal in Python

You can use one of the following two methods to convert and print a python string as an hexadecimal:

  1. Use the binascii library and the hexlify method.
  2. Convert the Python string to an integer, using the int() function, and then convert to an hexadecimal using hex().
  3. Turn the string to an integer using the literal_eval function (from the ast Python module) and then cast to hexadecimal.

#1: Using hexlify

Hexlify is a function available in both Python 2 and 3. Make sure to import the binascii library before using it. Hexlify allows us to encode our string as bytes, usually with utf-8 encoding. If an hex string is needed we need to decode it.

import binascii
my_str = "Python is cool"

# encode as bytes
my_hex_rep = binascii.hexlify(my_str.encode())

# decode to ascii
my_hex_str = my_hex_rep.decode('ascii')

print(my_hex_str)

#This will return the following result: 507974686f6e20697320636f6f6c

# 2: Using the int() function:

Using int(string, base=16) , we can convert the string to an integer with base 16 (Hexadecimal). Once we have the integer, we can use the inbuilt hex() function to convert the integer to a Hexadecimal number. So when we get a string, we will initially convert the string to an integer. We will then convert the integer value to hexadecimal using the function hex(). Let us see it in action.

Code:

def get_hex(value):
    convert_string = int(value, base=16)
    convert_hex = hex(convert_string)
    return convert_hex, convert_string

userstring = "0XABC"
convert_hex, convert_string = get_hex(userstring)
print("String to Integer:",convert_string)
print("Integer to Hex:",convert_hex)

Output:

String to Integer: 2748
Integer to Hex: 0xabc

# 3: Using ast.literal_eval() function:

Using the literal_eval from the ast library, we can easily get the string and convert it to an integer. Then we can use the hex() function to get the Hexadecimal value. All we have to do is that we will need to import the literal_eval function from ast. Let us look at an example.

Code:

from ast import literal_eval
def get_hex(value):
    convert_string = literal_eval(value)
    convert_hex = hex(convert_string)
    return convert_hex, convert_string
userstring = "0xabc"
convert_hex, convert_string = get_hex(userstring)
print("String to Integer:",convert_string)
print("Integer to Hex:",convert_hex)

Output:

String to Integer: 2748
Integer to Hex: 0xabc

Print string as bytes in Python

In order to cast and print a Python string to bytes, use the bytes() function, as shown in the code below:

file_str = 'Python string object'
print (bytes (file_str, encoding='windows-1255'))

Note: Use the decode() function to turn bytes to a string object.

FAQ

Can i convert the content of an entire file to hex?

Sure, you can easily open the file in a binary mode (using open()) and then use hexlify() or hex() functions to convert the bytes object.

Does hexlify() handle special characters?

Yes. All characters, including non printable characters can be converted to hex.

Can i convert to hexadecimal without using external libraries?

Yes, you can use the hex() function, that is available in core Python 3.

Follow up learning