1 hexstr、bytes、str相互转换
h:十六进制编码数字字符串
b:字节字符串
s: 字符串
Str to bytes:
bytes(s, encoding = “utf8”)
str.encode(s)
bytes to str:
str(b, encoding = “utf-8”)
bytes.decode(b)
hexstr to bytes
b = bytes.fromhex(h)
bytes to hexstr
h = b.hex()
str to bytes to hexstr
h = str.encode(s). hex()
hexstr to bites to str
h = bytes.decode(bytes.fromhex(h))
在这里插入代码片
```输入:
s ="12345"
print(s)
print(bytes(s, encoding = "utf8")) #str to bytes
print(str.encode(s)) #bytes
print(bytes(s, encoding = "utf8").hex()) #str to hex
print(str.encode(s). hex()) #str to hexstr
输出结果:
12345
b'12345'
b'12345'
3132333435
3132333435