title: compute CRC of struct in Python
body: <p>I have the following struct, from the NRPE daemon code in C:</p>
  
  <pre><code>typedef struct packet_struct {
    int16_t packet_version;
    int16_t packet_type;
    uint32_t crc32_value;
    int16_t result_code;
    char buffer[1024];
  } packet;
  </code></pre>
  
  <p>I want to send this data format to the C daemon from Python. The CRC is calculated when <code>crc32_value</code> is <code>0</code>, then it is put into the struct. My Python code to do this is as follows:</p>
  
  <pre><code>cmd = '_NRPE_CHECK'
  pkt = struct.pack('hhIh1024s', 2, 1, 0, 0, cmd)
  # pkt has length of 1034, as it should
  checksum = zlib.crc32(pkt) &amp; 0xFFFFFFFF
  pkt = struct.pack('hhIh1024s', 2, 1, checksum, 0, cmd)
  socket.send(....)
  </code></pre>
  
  <p>The daemon is receiving these values: <code>version=2  type=1  crc=FE4BBC49  result=0</code></p>
  
  <p>But it is calculating <code>crc=3731C3FD</code></p>
  
  <p>The actual C code to compute the CRC is:</p>
  
  <p><a href="https://github.com/KristianLyng/nrpe/blob/master/src/utils.c" rel="nofollow">https://github.com/KristianLyng/nrpe/blob/master/src/utils.c</a> </p>
  
  <p>and it is called via:</p>
  
  <p><code>calculate_crc32((char *)packet, sizeof(packet));</code></p>
  
  <p>When I ported those two functions to Python, I get the same as what <code>zlib.crc32</code> returns.</p>
  
  <p>Is my <code>struct.pack</code> call correct? Why is my CRC computation differing from the server's?</p>