2013-02-05 54 views
0

我有写入unsigned char数据块C++应用程序。所以我会写unsigned char data[8]蟒:将字符串转换为c_ubyte_Array_8

现在,我正在使用python(在python中读取​​的功能),在我的工具中读取并缓冲它以进行进一步处理。

问题
当我从文件中读取数据,并把它分解成8块,所有得到的数据是串format.I具有以下结构

class MyData(Union): 
    _fields_=[ ("data",8 * c_ubytes), ("overlap", SelfStructure) ] 

现在,我试图通过如下数据

dataObj = MyData(str[0:8]) 

它会抛出一个错误,expected c_ubyte_Array_8 instance, got str。我想我需要将string转换为array of size 8 of c_ubyte。试用bytearray但没有成功。请让我知道该怎么做。

回答

2

试试这个:

(ctypes.c_ubyte * 8)(*[ctypes.c_ubyte(ord(c)) for c in str[:8]]) 
+0

谢谢!完美工作。 –