【问题标题】:python ctypes bitwise data packingpython ctypes按位数据打包
【发布时间】:2013-07-03 09:44:40
【问题描述】:
item = -35519      
data_in = ctypes.c_int16(item)
data_pkd = (ctypes.c_int32(0) | data_in)

我遇到了错误

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 位测试数据发送到接受 int32 作为输入的 C 应用程序,如上述数据格式中所述。

谢谢

【问题讨论】:

    标签: python python-2.7 bit-manipulation ctype


    【解决方案1】:

    您不需要按位或,只需将 16 位值打包成 32 位值,即提升:

    data_pkd = ctypes.c_int32(data_in.value)
    

    要实际对 ctypes 值执行按位或运算,请对其 value 属性进行操作:

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

    【讨论】:

      猜你喜欢
      • 2022-10-25
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2017-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多