Signed equivalent of a 2's complement hex value
问题 On the python terminal when I do :- In [6]: 0xffffff85 Out[6]: 4294967173 In [9]: "%d" %(0xffffff85) Out[9]: '4294967173' I'd like to be able to give in 0xffffff85 and get the signed equivalent decimal number in python(in this case -123 ). How could I do that? In C, I could do it as :- int main() { int x = 0xffffff85; printf("%d\n", x); } 回答1: You could do that using ctypes library. >>> import ctypes >>> ctypes.c_int32(0xffffff85).value -123 You can also do the same using bitstring library. >