Loading

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

 1 # bytes object  
 2 b = b"example"  
 3 
 4 # str object  
 5 s = "example"  
 6 
 7 # str to bytes  
 8 bytes(s, encoding = "utf8")  
 9 
10 # bytes to str  
11 str(b, encoding = "utf-8")  
12 
13 # an alternative method  
14 # str to bytes  
15 str.encode(s)  
16 
17 # bytes to str  
18 bytes.decode(b)  

 

posted @ 2016-09-13 18:38  王树燚  阅读(534)  评论(0编辑  收藏  举报