python中str、bytes、十六进制字符串的相互转换

1. 基本概念

python中str、bytes、十六进制字符串的介绍

  1. str是python的内置函数,str字符串在python中使用''或者""括起来的字符串,比如:
    a = "hello world"
    
  2. bytes也是python的内置函数,bytes字符串通常以b开头,比如:
    byte_str = b'hello world'
    
  3. 十六进制字符串是指字符串中每个字符表示一个十六进制数,两个字符组成一个字节,比如:
    hex_str = '68656c6c6f20776f726c64'
    

    注:十六进制字符串的特点是不会出现f之后的字符,如果出现了hi,则无法转换成十六进制数

2.三者之间的转换方式

  1. 使用binascii模块
    binascii模块官方文档:https://docs.python.org/3.9/library/binascii.html#binascii.hexlify
    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
    
  2. 工作中可能还会遇到这种字符串,如b’xe5x97xa8xe5xaexa2’,这种是因为原始的字符不属于ascii字符集。utf-8是一个编码系统,可以与ascii兼容,可参考https://www.freecodecamp.org/chinese/news/what-is-utf-8-character-encoding/