字符串 Base64
import base64
st = 'hello world!'.encode("utf-8")
encode_bs64 = base64.b64encode(st)
# 结果bytes类型
print(encode_bs64)
# bytes => str
encode_bs64 = str(encode_bs64, encoding="utf-8")
print(encode_bs64)
a = 'aGVsbG8gd29ybGQh'
decode_bs64 = base64.b64decode(a).decode("utf-8")
print(decode_bs64)
数组 Base64
import base64
import numpy as np
def base64_encode(byte_arr):
"""
数组 转 Base64
@param byte_arr:
@return: base64_str
"""
base64_str = base64.b64encode(np.array(byte_arr, dtype=np.uint8)).decode('utf-8')
return base64_str
def base64_decode(base64_str):
"""
Base64 转 数组
@param base64_str:
@return: byte_arr
"""
byte_arr = np.frombuffer(base64.b64decode(base64_str), np.uint8)
return byte_arr
网友评论