python how to convert hex value to string?

There are several ways in Python to convert a hexadecimal value to a string, three of which are described below.

1. bytes.fromhex()How

bytes.fromhex()method converts a hexadecimal string to a byte string, and then converts the byte string to a string. For example:

hex_str = "48656c6c6f20576f726c64"
byte_str = bytes.fromhex(hex_str)
str_result = byte_str.decode("utf-8")

print(str_result)  # 输出:Hello World

Among them, bytes.fromhex()the method converts a hex string to a byte string, and byte_str.decode("utf-8")the method converts a byte string to a string.

It should be noted that bytes.fromhex()the method can only handle hexadecimal strings without spaces. If the input hexadecimal string contains spaces, the spaces need to be deleted first, for example:

hex_str = "48 65 6c 6c 6f 20 57 6f 72 6c 64"
hex_str = hex_str.replace(" ", "")  # 删除空格
byte_str = bytes.fromhex(hex_str)
str_result = byte_str.decode("utf-8")

print(str_result)  # 输出:Hello World

2. binascii.unhexlify()How

binascii.unhexlify()method converts a hexadecimal string to a byte string, and then converts the byte string to a string. For example:

import binascii

hex_str = "48656c6c6f20576f726c64"
byte_str = binascii.unhexlify(hex_str)
str_result = byte_str.decode("utf-8")

print(str_result)  # 输出:Hello World

Among them, binascii.unhexlify()the method converts a hex string to a byte string, and byte_str.decode("utf-8")the method converts a byte string to a string.

It should be noted that binascii.unhexlify()the method can only handle hexadecimal strings without spaces. If the input hexadecimal string contains spaces, the spaces need to be deleted first, for example:

import binascii

hex_str = "48 65 6c 6c 6f 20 57 6f 72 6c 64"
hex_str = hex_str.replace(" ", "")  # 删除空格
byte_str = binascii.unhexlify(hex_str)
str_result = byte_str.decode("utf-8")

print(str_result)  # 输出:Hello World

3. codecs.decode()How

codecs.decode()method can convert a hexadecimal string directly to a string. For example:

import codecs

hex_str = "48656c6c6f20576f726c64"
str_result = codecs.decode(hex_str, "hex").decode("utf-8")

print(str_result)  # 输出:Hello World

Among them, codecs.decode()the method converts a hexadecimal string directly to a byte string, and byte_str.decode("utf-8")the method converts a byte string to a string.

It should be noted that codecs.decode()the method can only handle hexadecimal strings without spaces. If the input hexadecimal string contains spaces, the spaces need to be deleted first, for example:

import codecs

hex_str = "48 65 6c 6c 6f 20 57 6f 72 6c 64"
hex_str = hex_str.replace(" ", "")  # 删除空格
str_result = codecs.decode(hex_str, "hex").decode("utf-8")

print(str_result)  # 输出:Hello World

All three methods can convert hexadecimal values ​​to strings, which method to choose depends on your use case and preference. It should be noted that if the hexadecimal string is invalid, these methods will throw an exception, and it is necessary to check the validity of the input hexadecimal string.

Guess you like

Origin blog.csdn.net/m0_72605743/article/details/129764199