python3字符串base64编解码


首先,Base64生成的编码都是ascii字符。

其次,python3中字符都为unicode编码,而b64encode函数的参数为byte类型,所以必须先转码。

s = "你好"

bs = base64.b64encode(s.encode("utf-8")) # 将字符为unicode编码转换为utf-8编码
print(bs) # 得到的编码结果前带有 b
>>> b'5L2g5aW9' bbs
= str(base64.b64decode(bs), "utf-8") print(bbs) # 解码
>>> 你好 bs
= str(base64.b64encode(s.encode("utf-8")), "utf-8") print(bs) # 去掉编码结果前的 b
>>> 5L2g5aW9 bbs
= str(base64.b64decode(bs), "utf-8") print(bbs) # 解码
>>> 你好

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM