python - 如何将无符号字节数组转换为ctypes中的base64字符串

标签 python ctypes

我有一个从 C SDK 返回的图像缓冲区,

我可以写入本地镜像并将其读取为 base64 字符串,但这需要额外的步骤。

如何将字节数组直接转换为base64字符串,以便我可以在网络请求中发送它?

image = (ctypes.c_ubyte*s.dwDataLen)()
ctypes.memmove(image, s.pBuffer, s.dwDataLen)

我尝试使用base64.encodestring但收到此错误

TypeError: expected single byte elements, not '<B' from c_ubyte_Array_8716

最佳答案

您可以使用base64模块

import base64

with open("yourfile.ext", "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())

情况与Encoding an image file with base64类似

关于python - 如何将无符号字节数组转换为ctypes中的base64字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573585/

相关文章:

python - 将脚本升级到 Python 3 -\r\n & 文本二进制模式

Python - 整数分解为素数

python - 如何在Python中的数据集的两个不同列中找到相同值的匹配

Python ctypes : calling a function with custom types in c

python - 在命名空间中启动 multiprocessing.process

python - 如何使用 ctypes 读取打包结构

python - 在庞大的数组中寻找最近的数组

python - Django 单元测试和全局固定装置

python - 将 c 结构体返回给 python 时出错

Python NtQueryDirectoryFile(文件信息结构)