Shivani Sharma — August 17, 2021

This article was published as a part of the Data Science Blogathon

## Objective

The hexadecimal base is 16, and we can represent the string in hexadecimal format using the 0x prefix. We can convert the string to hex using multiple methods. Let’s see some of them.

## Table of Content

1. Method 1: using hex ()

2. Method 2: convert string to hex using encode ()

3. Method 3. Using the ast.literal_eval () method

4. Error Exception (TypeError):

5. Conclusion

## Method 1: using hex ()

We can convert the string to hex using the hex () method. The hex () method takes a parameter in integer form, and for this, we first have to convert the string to an integer and then pass that value to the hex () method as shown below:

Example: string_to_hex.py

```# string_to_hex.py
str  =  "245FC"
# pass the str to the int () function to perform type casting and  to convert it into base16
basehexINT  =  int ( str ,  16 )
print ( "val" , basehexINT )
print ("val" , type (basehexINT))
hex_value  =  hex (basehexINT)
print (hex_value)
# checking the type of the value
print (type (hex_value))```

### Output:

```value  148988
value
0x245fc```

Line 3: We’ve created a line for the demo.

Line 6: We pass this string to the int () method with base 16. Now, this int () method converts the string to a hexadecimal integer value.

Line 9: We are printing the value we get after converting the string to an integer hexadecimal number.

Line 10: We are also printing the value type to confirm that the line is now in integer form.

Line 12: We know that the built-in hex (n) method takes an integer value, converting the integer to a hexadecimal string. This is why we need to convert the string to an integer in order to pass it to the hex () method. We passed this base16INT to the hex () method and got the hex_value string.

Line 13: We are printing this converted hexadecimal value.

So now we have converted the string to a hexadecimal value.

## Method 2: convert string to hex using encode ()

We can also convert a regular string to a hexadecimal string that has no hexadecimal characters. While doing this, you need to convert the string to a byte with the help of the encoding () method, and then finally you can convert the string to hex as shown below:

```# string_to_hex_utf8.py
# convert the string to the bytes
str =  'linuxhint' . encode ( 'utf-8' )
# print the converted string to bytes
print ( str )
# we use the below function to convert the bytes string to the hexadecimal string
hex_str  =  str . hex ( )
# print the converted hexadecimal value type
print ( type ( hex_str ) )```

### Output:

`b 'linuxhint'`

Lines 4 through 7: We have created a string that does not contain hexadecimal characters. And then using the encode () method we convert those strings to bytes. We then print those bytes that we see on line 1 of output.

Lines 10 through 13: Using the dot operator we call the hex () method, and according to its functioning it converts the bytes to the required hexadecimal string value. To confirm the type of the resulting string, we simply print line number 13 and the output shows that it is a hexadecimal string.

## Method 3. Using the ast.literal_eval () method

We can also convert a string to an integer using the ast literal_eval library method. This method also converts a string to an integer in order to use the hex () method to convert the string to a hexadecimal string. But only 0x prefix characters are accepted by this method.

```# string_to_hex_utf8.py
from  ast  import  literal_eval
str  =  "0xAAA"
# convert the string to the integer
conv_str  =  literal_eval ( str )
# print the value and type of the conv_str
print ( conv_str )
print ( "type" ,  type ( conv_str ) )
# pass the conv_str to the hex () method
hex_value  =  hex ( conv_str )
print ( hex_value )
# chcking the type of the value
print ( type ( hex_value ) )```

### Output:

```2730
type
0xaaa```

Lines 2-10: We import the literal_eval () method from the ast library. Then we create a string prefixed with 0x. We then passed that string to the literal_eval () method and converted it to an integer. To confirm that the output is in integer form, we print it at line 9. We also print the output type, which indicates that it is an integer.

Lines 13 through 17: The well-known built-in hex (n) method is used here, which takes an integer value as an input and converts that integer value to a hexadecimal string as an output. This is why we need to convert the string to an integer in order to pass it to the hex () method. We passed this value to convert_str (integer) to the hex () method and got hex_value, a hexadecimal string. We also print this converted hexadecimal value. We also print the type of the converted hexadecimal value, which indicates that it is a hexadecimal string type.

## Error Exception (TypeError):

Sometimes we get errors when converting a string to hexadecimal. The reason is that the hex () method only takes an integer value as a parameter. And we try to give them the input in many other formats like float, double, bytes, and sometimes in String as well. That’s why the compilation error occurs simultaneously. While using any built-in function in any language the more focusing thing is the data types of its parameters because without the proper knowledge of data type we can’t use any built-in library or functions.

```string_hex  =  "0xFF"
hex_output  =  hex ( string_hex )
print ( hex_output )```

### Output:

TypeError: ‘str’ object cannot be compiled as an integer or float

## Conclusion

So, we have seen various methods for converting a string to a lowercase hexadecimal string. The hex () method is very popular for its ease of use. But sometimes we want to convert the string without using the 0x prefix, so in this case, we can use the bytes encode () method as we saw in the article. There are many other approaches available for the same but these are the more efficient ones. For any further queries just hit the comment box or you can also contact me at LinkedIn at https://www.linkedin.com/in/shivani-sharma-aba6141b6/

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.