Two's complement of Hex number in Python

A nice way to do this in Python is using bitwise operations. For example, for 32-bit values:

def s32(value):
    return -(value & 0x80000000) | (value & 0x7fffffff)

Applying this to your values:

>>> s32(a)
398969966
>>> s32(b)
-1051154348

What this function does is sign-extend the value so it's correctly interpreted with the right sign and value.

Python is a bit tricky in that it uses arbitrary precision integers, so negative numbers are treated as if there were an infinite series of leading 1 bits. For example:

>>> bin(-42 & 0xff)
'0b11010110'
>>> bin(-42 & 0xffff)
'0b1111111111010110'
>>> bin(-42 & 0xffffffff)
'0b11111111111111111111111111010110'

why not using ctypes ?

>>> import ctypes
>>> a = 0x17c7cc6e
>>> ctypes.c_int32(a).value
398969966
>>> b = 0xc158a854
>>> ctypes.c_int32(b).value
-1051154348

>>> import numpy
>>> numpy.int32(0xc158a854)
-1051154348