>我用C编写了以下代码
short a = 0xFFFE;
printf("hex = 0x%X, signed short = %dn", a & 0xFFFF, a);
Output ---> hex = 0xFFFE, signed short = -2
现在使用 ctypes 在 Python 中做同样的事情
from ctypes import *
mc = cdll.msvcrt
a = c_short(0xFFFE)
mc.printf("hex = 0x%X, signed short = %dn", a, a)
Output ----> hex = 0xFFFE, signed short = 65534
我不确定为什么输出不同?知道吗?
printf
没有被正确调用。 使用%hX
和%hd
通过短裤。
>>> from ctypes import *
>>> mc = cdll.msvcrt
>>> a=c_short(0xFFFE)
>>> mc.printf('hex=0x%hX, signed short=%hdn',a,a)
hex=0xFFFE, signed short=-2
28