繁体   English   中英

Python:字节数组到 ctypes 数组

[英]Python: bytearray to ctypes array

我正在尝试将bytearray写入 ctypes 结构的 ctypes c_uint8缓冲区

class RpRes(LittleEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("count", c_uint16),
        ("buf", c_uint8 * 512)
    ]

def read_func(req):
    res = RpRes()
    buf = os.read(req.fd, req.count)
    res.buf.from_buffer(buf)
    res.count = len(buf)
    return res

res.buf.from_buffer(buf)给出以下错误:

AttributeError: 'c_ubyte_Array_512' object 没有属性 'from_buffer'

如何实现?

这对我有用。

def read_func(req):
    res = RpRes()
    buf = os.read(req.fd, req.count)
    res.buf = (c_uint8 * sizeof(res.buf))(*buf)
    res.count = len(buf)
    return res

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM