# 将16进制字符串转换为普通字符串的示例代码
# 示例1: 使用bytes.fromhex()方法
hex_string = "48656c6c6f20576f726c64" # 这是 "Hello World" 的16进制表示
byte_data = bytes.fromhex(hex_string)
string_data = byte_data.decode("utf-8")
print(string_data) # 输出: Hello World
# 示例2: 使用binascii.unhexlify()方法
import binascii
hex_string = "48656c6c6f20576f726c64" # 这是 "Hello World" 的16进制表示
byte_data = binascii.unhexlify(hex_string)
string_data = byte_data.decode("utf-8")
print(string_data) # 输出: Hello World
bytes.fromhex()
方法:
decode("utf-8")
将字节对象解码为普通的字符串。binascii.unhexlify()
方法:
binascii
模块。decode("utf-8")
将字节对象解码为普通的字符串。这两种方法都可以实现从16进制字符串到普通字符串的转换。选择哪种方法取决于你的具体需求和偏好。
上一篇:python中的字符串
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站