title: Two's complement of Hex number in Python
body: <p>Below a and b (hex), representing two's complement signed binary numbers.
  For example:</p>
  
  <pre><code>a = 0x17c7cc6e
  b = 0xc158a854
  </code></pre>
  
  <p><em>Now I want to know the signed representation of a &amp; b in base 10.</em> Sorry I'm a low level programmer and new to python; feel very stupid for asking this. I don't care about additional library's but the answer should be simple and straight forward. Background: a &amp; b are extracted data from a UDP packet. I have no control over the format. So please don't give me an answer that would assume I can change the format of those varibles before hand.</p>
  
  <p>I have converted a &amp; b into the following with this:</p>
  
  <pre><code>aBinary = bin(int(a, 16))[2:].zfill(32) =&gt; 00010111110001111100110001101110 =&gt; 398969966
  bBinary = bin(int(b, 16))[2:].zfill(32) =&gt; 11000001010110001010100001010100 =&gt; -1051154348
  </code></pre>
  
  <p>I was trying to do something like this (doesn't work):</p>
  
  <pre><code>if aBinary[1:2] == 1:
  aBinary = ~aBinary + int(1, 2)
  </code></pre>
  
  <p><strong>What is the proper way to do this in python?</strong></p>