gpt4 book ai didi

python - 在 Python 中将 byte[] 转换为 base64 和 ASCII

转载 作者:太空狗 更新时间:2023-10-29 21:30:37 25 4
gpt4 key购买 nike

如何将字节数组转换为 base64 字符串和/或 ASCII。

我可以在 C# 中轻松完成此操作,但似乎无法在 Python 中执行此操作

最佳答案

最简单的方法是:数组到 json 到 base64:

import json
import base64

data = [0, 1, 0, 0, 83, 116, -10]
dataStr = json.dumps(data)

base64EncodedStr = base64.b64encode(dataStr.encode('utf-8'))
print(base64EncodedStr)

print('decoded', base64.b64decode(base64EncodedStr))

打印出来:

>>> WzAsIDEsIDAsIDAsIDgzLCAxMTYsIC0xMF0=
>>> ('decoded', '[0, 1, 0, 0, 83, 116, -10]') # json.loads here !

...另一个选项可能是使用 bitarray module .

关于python - 在 Python 中将 byte[] 转换为 base64 和 ASCII,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43920799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com