python:如何将列表转换为c_byte数组

时间:2014-01-06 10:56:50

标签: python arrays list

我在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数组?

1 个答案:

答案 0 :(得分:2)

你可以这样做 -

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