Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python ctypes按位数据打包_Python_Python 2.7_Bit Manipulation_Ctype - Fatal编程技术网

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)