参考链接
如何将 python数据类型按照 ctypes 类型转换位 bytes
from ctypes import *
class C_class1(Structure):
_pack_ = 1
_fields_ = [('value', c_byte)]
cmd = C_class1()
cmd.value = -2
ret = (c_byte * sizeof(cmd))()
memmove(addressof(ret), addressof(cmd), sizeof(cmd))
print(bytearray(ret))
如何将 bytes 按照 ctypes 类型转换为 python 数据类型
from ctypes import *
class C_class1(Structure):
_pack_ = 1
_fields_ = [('value', c_byte)]
b_arr = bytearray(b'\xfe')
cmd = C_class1()
ctypes_arr = (c_byte * sizeof(cmd)).from_buffer(b_arr)
memmove(addressof(cmd), addressof(ctypes_arr), sizeof(cmd))
print(cmd.value)