我在dll中有一个C函数。
它具有int dcm(char inmsg[], int length);
之类的界面
现在我有一个列表(有时成员数超过1000)
a = [0x41,0x00,0x00,0xC8,0x08,0x01,0x03,0x00,0x00,0x02,0x10,0x00,0x66,0x62,0x05,0x00,0x06,0x00,0x57,0x02,0x01,0xBF,0xFF,0x00,0xBF,0x9A,0x00,0x05,0x80,0x10,0x32,0x00,0x07,0x1F,0x00,0x00,0x05,0x00,0xD1,0x01,0x01,0xBF,0x04,0x00,0x03,0xBF,0x04,0x00,0x21,0x64,0x00,0x00,0x0A,0xBF,0xFF,0x00,0xBF,0xFF,0x00,0xBF,0xFF,0x00,0xBF,0xF3,0x00]
我想将列表转换为
c_byte
数组,并将其作为函数dcm
的第一个参数传递。如何将其转换为c_byte数组?
最佳答案
你可以这样做 -
import ctypes
a = [0x41,0x00,0x00,0xC8,0x08,0x01,0x03,0x00,0x00,0x02,0x10,0x00,0x66,0x62,0x05,0x00,0x06,0x00,0x57,0x02,0x01,0xBF,0xFF,0x00,0xBF,0x9A,0x00,0x05,0x80,0x10,0x32,0x00,0x07,0x1F,0x00,0x00,0x05,0x00,0xD1,0x01,0x01,0xBF,0x04,0x00,0x03,0xBF,0x04,0x00,0x21,0x64,0x00,0x00,0x0A,0xBF,0xFF,0x00,0xBF,0xFF,0x00,0xBF,0xFF,0x00,0xBF,0xF3,0x00]
arr = (ctypes.c_byte * len(a))(*a)
然后,您可以将
arr
传递给C函数。更多详细信息是in the doc。关于python - python:如何将列表转换为c_byte数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20948490/