title: CRC-32 with LFSR Byte by Byte in C
body: <p>I implemented a CRC32 algorithm in C but after hours of looking around and trying to fix it, it still doesn't work. It compiles but the checksum is not the same as in several online CRC calculators. I suspect the error to be in the line "if (0 != (character &amp; j))" but don't understand it, since it is even mainly the same as the code on wikipedia.</p>
  
  <p><a href="http://de.wikipedia.org/wiki/Zyklische_Redundanzpr%C3%BCfung#Berechnung_einer_CRC-Pr.C3.BCfsumme_in_C_und_Pascal.2FDelphi" rel="nofollow">wikipedia</a>    </p>
  
  <pre><code>int CRC32_C(char *message){
      int length, i, j;
      unsigned long p,g;
      char character;
      p = 0x00000000;
      g = 0x04C11DB7;
      length = strlen(message);
  
      for (i = 0; i &lt; length; i++){
          character=*message;
          //iterieren durch die bits jedes zeichens
          for (j= 0x80; j != 0; j &gt;&gt;= 1){ 
              //(p &amp; 0x80000000)? 1 : 0)
              if (0 != (character &amp; j))
                  p = (p &lt;&lt; 1) ^ g;
              else
                  p &lt;&lt;=1;
          }
          message++;
      }
      return p;
  }
  //sample main
  int main(char ** argv, int argc){
      char *msg;
      int p;
      msg = "HALLO";
      p = CRC32_C(msg);
      printf("p ist 0x%x \n", p);
      return 0;
  }
  </code></pre>
  
  <p>Sample input: "HALLO"</p>
  
  <p>Expected result: 0x4E26F361 (according to <a href="http://www.lammertbies.nl/comm/info/crc-calculation.html" rel="nofollow">this page</a>, which uses the same generator polynomial, as seen at the bottom of the page)</p>
  
  <p>Actual result: 0xc25a747d</p>
  
  <p>@chux: I tried removing the "0 !=" in the if clause, but it didn't change the result.</p>
  
  <p>CRC32_C simply stands for "Implemented in C". As the Generator polynomial suggests, it is standard Ethernet.</p>
  
  <p>Thanks for your help</p>