Bitrex Bitrex - 6 months ago 11
Python Question

Casting between integer formats with Python ctypes

How can I use the ctypes "cast" function to cast one integer format to another, to obtain the same effect as in C:

int var1 = 1;
unsigned int var2 = (unsigned int)var1;


?

Answer
>>> cast((c_int*1)(1), POINTER(c_uint)).contents
c_uint(1L)
>>> cast((c_int*1)(-1), POINTER(c_uint)).contents
c_uint(4294967295L)