2013-07-03 33 views
-1
item = -35519  
data_in = ctypes.c_int16(item) 
data_pkd = (ctypes.c_int32(0) | data_in) 

我正在下面誤差蟒ctypes的逐位數據打包

data_pkd = (ctypes.c_int32(0) | data_in) 
TypeError: unsupported operand type(s) for |: 'c_long' and 'c_short' 
|31||30| 29 28 27 26 25 24 23 22 21 20 19 18 17 16| 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0| 
|P|M|------------------unused-------------------------------------|------------------------------item----------------------------| 

我意向32位測試數據發送至C應用程序接受INT32作爲輸入,在上述數據格式提及。

感謝

回答

0

你不需要按位或,只是包裝的16位值轉換成32位的一個,即促進:

data_pkd = ctypes.c_int32(data_in.value) 

實際執行按位或ctypes值,對它們的值屬性進行操作:

x = ctypes.c_int16(...) 
y = ctypes.c_int32(...) 
data_pkd = ctypes.c_int32(x.value | y.value)