Python - string bytes hex进制字符串间的相互转换

import binascii

# 字符串转为 bytes
emp = "So Amazing"  # string
b_emp = emp.encode("utf-8")  # string to bytes

# bytes 转换为 hex string
hexstr_emp = b_emp.hex()  # bytes to hex string
b_hexstr_emp = binascii.hexlify(b_emp)  # bytes to hex string bytes
hexstr_emp = b_hexstr_emp.decode("utf-8")  # decode hex string bytes to hex string

# hex string 转换为 bytes
b_emp = bytes.fromhex(hexstr_emp)  # hex string to string bytes
b_emp = binascii.unhexlify(hexstr_emp)  # hex string to string bytes

# bytes 转换为 string
emp = b_emp.decode()  # decode string bytes to string

你可能感兴趣的:(Python,python)