将int转换为16位非单值short

Mil*_*are 5 python

我想在Python中将整数修整为16字节的单词(无符号短)。像这样的事情不起作用

word = array("H")
word.insert(0,0x19c6acc6)
Run Code Online (Sandbox Code Playgroud)

Ash*_*ary 8

使用ctypes.c_ushort

>>> import ctypes
>>> word.insert(0, ctypes.c_ushort(0x19c6acc6).value)
>>> word
array('H', [44230])
Run Code Online (Sandbox Code Playgroud)

如果 NumPy 可用,则:

>>> numpy.ushort(0x19c6acc6)
44230
Run Code Online (Sandbox Code Playgroud)


Nik*_* B. 6

经典的方法是使用掩码提取相关位:

>>> hex(0x19c6acc6 & 0xffff)
'0xacc6'
Run Code Online (Sandbox Code Playgroud)