Python:bytearray到ctypes数组

2024-06-26 05:06:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将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 has no attribute 'from_buffer'

如何做到这一点


Tags: fromreadbuffercountresctypes结构req