python:将bytearray转换为ctypes Stru

2024-06-01 12:46:50 发布

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

我有一个n字节的字节。这与定义的ctypes.Structure之一相同。我想把这个小房间打成这样。以便我可以访问每个成员。我该怎么做?

class ABC(Structure):
    _fields_ = [("a", c_uint), ("b", c_ushort), ("c", c_ushort)]

class DEF(Structure):
    _fields_ = [("abc", ABC), ("i", I)]

b = bytearray(b'\x88\x08\xc0\xf9\x02\x85\x10\x00\xcc')

s = DEF()
print(s.abc.a)

如何获得上述print语句的正确值?


Tags: fields字节定义def成员ctypesstructureclass