1、参考:https://www.jianshu.com/p/5bb986772ef8
https://www.cnblogs.com/longyuu/p/14180127.html
2、示例
t1 = 'test'.encode()
type(t1)
# <class 'bytes'>
t2 = b'test'.decode()
type(t2)
# <class 'str'>
t3 = b'test'.hex()
type(t3)
# <class 'str'>
my_hex = '\x53\x00\x45\x00\x54\x00\x41\x00\x42\x00\x4c\x00\x45\x00\x20\x00\x4f\x00\x4e\x00'
type(my_hex)
# <class 'str'>
t4 = "\\".join(hex(ord(n)) for n in my_hex)
type(t4)
# <class 'str'>
t5 = bytes([ord(n) for n in my_hex])
type(t5)
# <class 'bytes'>
t6 = t5.decode('utf-16')
print(t6)
# SETABLE ON
3、示例2
soc_bytes= bytearray()
soc_bytes.append(0x1C)
soc_bytes.append(0x03)
soc_bytes.append(0x09)
soc = " ".join(hex(n) for n in soc_bytes)
soc1 = " ".join(f"{n:02x}" for n in soc_bytes)
print(f"发送消息: {soc}")
print(f"发送消息1: {soc1}")
print(":".join(hex(n) for n in b"TCB"))
print(":".join(f"{n:02X}" for n in b"TCB"))