240
世界上有10种人,一种懂二进制,另一种不懂二进制。

python字符串str和字节数组bytes相互转化

1 引言

后续待补充

2 代码

b = b"Hello, world!"  # bytes  
s = "Hello, world!"   # string

print('str --> bytes')
print(bytes(s, encoding="utf8"))   
print(bytes(s))  #默认utf8编码

print('bytes --> str')
print(str(b, encoding="utf-8"))  
print(str(b))    #默认utf8编码

 

posted @ 2018-11-27 14:05  unionline  阅读(9492)  评论(0编辑  收藏  举报