python ctypes按位数据打包
我正在犯错误python ctypes按位数据打包,python,python-2.7,bit-manipulation,ctype,Python,Python 2.7,Bit Manipulation,Ctype,我正在犯错误 item = -35519 data_in = ctypes.c_int16(item) data_pkd = (ctypes.c_int32(0) | data_in) 我打算将32位测试数据发送到接受int32作为输入的C应用程序,如上述数据格式所述 谢谢您不需要按位或,只需将16位值打包为32位值,即升级: data_pkd = (ctypes.c_int32(0) | data_in) TypeError: unsupported operand type(s
item = -35519
data_in = ctypes.c_int16(item)
data_pkd = (ctypes.c_int32(0) | data_in)
我打算将32位测试数据发送到接受int32作为输入的C应用程序,如上述数据格式所述
谢谢您不需要按位或,只需将16位值打包为32位值,即升级:
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----------------------------|
要实际执行按位或按ctypes值,请对其值属性进行操作:
data_pkd = ctypes.c_int32(data_in.value)
x = ctypes.c_int16(...)
y = ctypes.c_int32(...)
data_pkd = ctypes.c_int32(x.value | y.value)