如何将无符号字节数组转换为ctypes中的base64字符串
How to convert unsigned byte array to base64 string in 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
类似
试试这个:
import ctypes
import base64
image = (ctypes.c_ubyte * s.dwDataLen)()
ctypes.memmove(image, s.pBuffer, s.dwDataLen)
# Convert the image to an array of bytes
buffer = bytearray(image)
encoded = base64.encodebytes(buffer)
如果您正在使用 base64.b64encode
,您应该能够将 image
传递给它:
import ctypes
import base64
image = (ctypes.c_ubyte * s.dwDataLen)()
ctypes.memmove(image, s.pBuffer, s.dwDataLen)
encoded = base64.b64encode(image)
我有一个从 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
类似试试这个:
import ctypes
import base64
image = (ctypes.c_ubyte * s.dwDataLen)()
ctypes.memmove(image, s.pBuffer, s.dwDataLen)
# Convert the image to an array of bytes
buffer = bytearray(image)
encoded = base64.encodebytes(buffer)
如果您正在使用 base64.b64encode
,您应该能够将 image
传递给它:
import ctypes
import base64
image = (ctypes.c_ubyte * s.dwDataLen)()
ctypes.memmove(image, s.pBuffer, s.dwDataLen)
encoded = base64.b64encode(image)