Python:Base64编码解码

import base64

s_raw = "中国人a"
b_raw = s_raw.encode() # S.encode(encoding='utf-8', errors='strict') -> bytes

b64 = base64.b64encode(b_raw)
print("b64", b64)
s64 = b64.decode()
print("s64", s64)


b = base64.b64decode(s64)
print("b", b)
s = b.decode("utf-8") 
print("s", s)

#输出
"""
b64 b'5Lit5Zu95Lq6YQ=='
s64 5Lit5Zu95Lq6YQ==
b b'\xe4\xb8\xad\xe5\x9b\xbd\xe4\xba\xbaa'
s 中国人a
"""

b64encodeb64decode接收参数为bytes或ascii码字符串,返回值为bytes。
字符串和bytes互相转换有encodedecode方法,默认编码为utf-8。

转载于:https://www.cnblogs.com/xuejianbest/p/10285120.html

你可能感兴趣的:(python)