python中str、bytes、十六进制字符串的介绍
''
或者""
括起来的字符串,比如:a = "hello world"
byte_str = b'hello world'
hex_str = '68656c6c6f20776f726c64'
注:十六进制字符串的特点是不会出现f之后的字符,如果出现了hi,则无法转换成十六进制数binascii.hexlify(data[, sep[, bytes_per_sep=1]])
Return the hexadecimal representation of the binary data.
Every byte of data is converted into the corresponding 2-digit hex representation.
The returned bytes object is therefore twice as long as the length of data.
binascii.unhexlify(hexstr)
Return the binary data represented by the hexadecimal string hexstr.
This function is the inverse of b2a_hex().
hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise an Error exception is raised.
binascii.hexlify会将二进制数据以十六进制的方式返回,binascii.unhexlify会将十六进制表示的数据以二进制的方式返回,具有相同作用的还有bytes.hex()
和bytes.fromhex()
import binascii
#将字符串转换成bytes类型
str_data = "hello world"
bytes_data = str_data.encode('utf-8')
print("bytes_data:", bytes_data)
# 将bytes类型转换成十六进制字符串
hex_data = binascii.hexlify(bytes_data)
print("hex_data:", hex_data)
hex_str = hex_data.decode('utf-8')
# 打印结果
print("hex_str:", hex_str)
# 将十六进制字符串转换成bytes类型
bytes_data = binascii.unhexlify(hex_str)
str_data = bytes_data.decode('utf-8')
# 打印结果
print(str_data)
响应结果:bytes_data: b'hello world'
hex_data: b'68656c6c6f20776f726c64'
hex_str: 68656c6c6f20776f726c64
hello world