printf
isn't being called correctly. Use %hX
and %hd
for passing shorts.
>>> from ctypes import *
>>> mc = cdll.msvcrt
>>> a=c_short(0xFFFE)
>>> mc.printf('hex=0x%hX, signed short=%hd\n',a,a)
hex=0xFFFE, signed short=-2
28
题
I wrote the following code in C
short a = 0xFFFE;
printf("hex = 0x%X, signed short = %d\n", a & 0xFFFF, a);
Output ---> hex = 0xFFFE, signed short = -2
Now tying to do the same in Python using ctypes
from ctypes import *
mc = cdll.msvcrt
a = c_short(0xFFFE)
mc.printf("hex = 0x%X, signed short = %d\n", a, a)
Output ----> hex = 0xFFFE, signed short = 65534
I am not sure why the output is different? Any idea?
解决方案
printf
isn't being called correctly. Use %hX
and %hd
for passing shorts.
>>> from ctypes import *
>>> mc = cdll.msvcrt
>>> a=c_short(0xFFFE)
>>> mc.printf('hex=0x%hX, signed short=%hd\n',a,a)
hex=0xFFFE, signed short=-2
28