title: Problem in calculating checksum : casting int to signed int32
body: <p>I need to convert the following c code (to calculate checksum for a file) to python. I had written, the corresponding code in python but the result didn't match the c version. The problem was that python autmatically promotes int to long whenever overflow occurs and this results in wrong checksums.</p>
  
  <p>Any idea how to overcome this problem ? or is there a python function that converts long to signed int32 ?</p>
  
  <p>Thanks</p>
  
  <pre><code>int calcChecksum(const guchar *data, gsize len)
  { 
  
      const guchar *p = data;
      int checksum = 0, g, i = len;
  
      while(i--) {
              checksum = (checksum &lt;&lt; 4) + *p++;
  
              if((g = (checksum &amp; 0xf0000000)) != 0)
                      checksum ^= g &gt;&gt; 23;
  
              checksum &amp;= ~g;
      }
      return checksum;
  }
  </code></pre>
  
  <p>Solution:</p>
  
  <p>Thanks for all the help. Here's the function that worked for me - </p>
  
  <pre><code> def int32(x):
      x = 0xffffffff &amp; x
      if x &gt; 0x7fffffff :
          return - ( ~(x - 1) &amp; 0xffffffff )
      else : return x 
  </code></pre>