Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top