Hexadecimal has a base of 16, and we can represent a string in hexadecimal format using the prefix 0x.
We can convert the string to hexadecimal using the following methods:
- Using the hex(n) method
- Using the encode () method
- Using the literal_eval () method
Method 1: Using the hex ()
We can convert the string to hexadecimal using the hex () method. The hex () method accepts the parameter in integer form, and for that first, we have to convert the string to an integer and then passed that value to the hex () method as shown below:
Example: string_to_hex.py
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# string_to_hex.py
str = "245FC" # pass the str to the int () to convert it into base16 int base16INT = int(str, 16) # print the converted string to base16 hexadecimal int value print("value",base16INT) print("value",type(base16INT)) hex_value = hex(base16INT) print(hex_value) # chcking the type of the value print(type(hex_value)) |
Output:
1
2 3 4 5 6 7 |
value 148988
value <class 'int'> 0x245fc <class 'str'> |
Line 3: We created a string for the demo.
Line 6: We pass that string to the int () method with the base 16. Now, this int () method will convert the string to the hexadecimal integer value.
Line 9: We print the value which we get after converting the string to integer hexadecimal.
Line 10: We also print the value type to confirm that the string is now in the form of an integer.
Line 12: We know the in-built hex(n) method accepts the integer value, converting the integer to the hexadecimal string. That’s why we need to convert the string to an integer to pass it into the hex () method. We passed that base16INT value to the hex() method and got the hex_value, string hexadecimal.
Line 13: We print that converted hexadecimal value.
Line 16: We print the type of the converted hexadecimal value, which shows in the output it is a string type.
So, now we converted the string to a hexadecimal value.
Method 2: Convert the string to hexadecimal using the encode ()
We can also convert the normal string to a hexadecimal string that doesn’t have any hexadecimal character. For that, we have to convert the string to byte using the method encode (), and then we can convert the string to hexadecimal as shown below:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# string_to_hex_utf8.py
# convert the string to the bytes str= 'linuxhint'.encode('utf-8') # print the converted string to bytes print(str) # convert the string bytes to the hexadecimal string hex_str = str.hex() # print the converted hexadecimal value type print(type(hex_str)) |
Output:
1
2 3 |
b'linuxhint'
<class 'str'> |
Line 4 to 7: We created a string that doesn’t have any hexadecimal character. And then convert those strings to bytes using the encode () method. And then, we print those bytes, which we can see in the output line number 1.
Line 10 to 13: We call the hex () method using the dot operator, and now the bytes are converted to the hexadecimal string value we required. To confirm the type of the result string, we just print the line number 13, and the output shows that it is in a string hexadecimal type.
Method 3. Using ast.literal_eval () method
We can also convert the string to an integer using the ast library method literal_eval. This method also converts the string to an integer to use the hex () method to convert the string to hexadecimal string. But this method only accepts 0x prefix characters.
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# string_to_hex_utf8.py
from ast import literal_eval str = "0xAAA" # convert the string to the integer convert_str = literal_eval(str) # print the value and type of the convert_str print(convert_str) print("type", type(convert_str)) # pass the convert_str to the hex () method hex_value = hex(convert_str) print(hex_value) # chcking the type of the value print(type(hex_value)) |
Output:
1
2 3 4 5 6 7 |
2730
type <class 'int'> 0xaaa <class 'str'> |
Line 2 to 10: We import the method literal_eval () from the ast library. Then we create a string with the prefix 0x. Then we passed that string to the literal_eval() method and converted it to the integer. To confirm that the output is in integer form, we print it in line number 9. We also print the type of the output, which shows it is an integer.
Line 13 to 17: We know the in-built hex(n) method that accepts the integer value, converting the integer to the hexadecimal string. That’s why we need to convert the string to an integer to pass it into the hex () method. We passed that convert_str(integer) value to the hex() method and got the hex_value, string hexadecimal. We print that converted hexadecimal value. We print the type of the converted hexadecimal value also, which shows it is a string hexadecimal type.
Error Exception (TypeError):
Sometimes we will get errors while converting the string to a hexadecimal string. The reason behind that is the hex () method only accepts the integer value as a parameter.
1
2 3 4 5 6 7 |
string_hex = "0xFF"
hex_output = hex(string_hex) print(hex_output) |
Output:
1
|
TypeError: 'str' object cannot be interpreted as an integer
|
Conclusion:
So, we have seen different methods to convert the string to a hexadecimal lowercase string. The hex () method is very popular because of its easy use. But sometimes, we want to convert the string without using the prefix 0x, so in that case, we can use the bytes encode () method, as we have already seen in the article.
The code of this article is available at the below github link:
from Linux Hint https://ift.tt/3l2Af3h
0 Comments