title: Cant get CRC function with Big Endian to work
body: <p>I am having trouble getting a CRC function to work in C#. Below is the example C code given, below is the C# code I use. I suspect the culprit is the big endian, but when I switch the byte order of the checksum it still gives different values. </p>
  
  <p>Any ideas where I am going wrong?</p>
  
  <p>Many thanks</p>
  
  <p>Matt</p>
  
  <p>CRC is calculated using CRC-CCITT (Kermit) algorithm (<a href="http://www.lammertbies.nl/comm/info/crccalculation.html" rel="nofollow">http://www.lammertbies.nl/comm/info/crccalculation.html</a>). </p>
  
  <p>Parameter length – 2 bytes. Format is big endian. That is why bytes 0 and 1 are “switched”.</p>
  
  <p>CRC calculation algorithm is shown below (C programming language).</p>
  
  <pre><code>/*----------------------------------------------------------------------------
   * FUNCTION: CRC16
   *---------------------------------------------------------------------------    
   */
  unsigned short crc_16_rec (unsigned char *pucData, unsigned short ucLen) {
      //--------------------------------------------------------------------
      unsigned int i;
      unsigned char ucBit, ucCarry;
      //--------------------------------------------------------------------
      unsigned short usPoly = 0x8408;//reversed 0x1021
      unsigned short usCRC = 0;
      //--------------------------------------------------------------------
      for (i = 0; i &lt; ucLen; i++) {
          usCRC ^= pucData[i];
          for (ucBit = 0; ucBit &lt; 8; ucBit++) {
              ucCarry = usCRC &amp; 1;
              usCRC &gt;&gt;= 1;
              if (ucCarry) {
                  usCRC ^= usPoly;
              }
          }
      }
      //--------------------------------------------------------------------
      return usCRC;
      //--------------------------------------------------------------------
  }
  </code></pre>
  
  <p>The code I use</p>
  
  <pre><code>public enum Crc16Mode : ushort { Standard = 0xA001, CcittKermit = 0x8408 }
  
  Crc16 CRC = new Crc16(Crc16Mode.CcittKermit);
  
  public class Crc16
      {
  
  
          readonly ushort[] table = new ushort[256];
  
          public ushort ComputeChecksum(params byte[] bytes)
          {
              ushort crc = 0;
              for (int i = 0; i &lt; bytes.Length - 2; ++i)
              {
                  byte index = (byte)(crc ^ bytes[i]);
                  crc = (ushort)((crc &gt;&gt; 8) ^ table[index]);
              }
              return crc;
          }
  
          public bool ChecksumValid(ref byte[] bytes)
          {
              ushort checksum = (ushort)((bytes[bytes.Length - 2] &lt;&lt; 8) | bytes[bytes.Length - 1]);
              ushort checksum_received = CRC16(bytes);
              if (checksum == checksum_received)
              {
                  return true;
              }
              else
              {
                  return false;
              }
          }
  
          public ushort CRC16(byte[] bytes)
          {
              ushort crc = 0; //(ushort.maxvalue, 65535)
  
              for (int j = 0; j &lt; bytes.Length; j++)
              {
                  crc = (ushort)(crc ^ bytes[j]);
                  for (int i = 0; i &lt; 8; i++)
                  {
                      if ((crc &amp; 0x0001) == 1)
                          crc = (ushort)((crc &gt;&gt; 1) ^ 0x8408);
                      else
                          crc &gt;&gt;= 1;
                  }
              }
              return (ushort)~(uint)crc;
          }
  
          public byte[] ComputeChecksumBytes(params byte[] bytes)
          {
              ushort crc = ComputeChecksum(bytes);
              return BitConverter.GetBytes(crc);
          }
  
          public Crc16(Crc16Mode mode)
          {
              ushort polynomial = (ushort)mode;
              ushort value;
              ushort temp;
              for (ushort i = 0; i &lt; table.Length; ++i)
              {
                  value = 0;
                  temp = i;
                  for (byte j = 0; j &lt; 8; ++j)
                  {
                      if (((value ^ temp) &amp; 0x0001) != 0)
                      {
                          value = (ushort)((value &gt;&gt; 1) ^ polynomial);
                      }
                      else
                      {
                          value &gt;&gt;= 1;
                      }
                      temp &gt;&gt;= 1;
                  }
                  table[i] = value;
              }
          }
      }
  </code></pre>