title: CRC16 ISO 13239 Implementation
body: <p>i'm trying to implement Crc16 in C#. I already tried many different implementations, but most of them gives me different values. Here are some of the codes that i already used.</p>
  
  <pre><code>    private static int POLYNOMIAL = 0x8408;
      private static int PRESET_VALUE = 0xFFFF;
  
      public static int crc16(byte[] data)
      {
          int current_crc_value = PRESET_VALUE;
          for (int i = 0; i &lt; data.Length; i++)
          {
              current_crc_value ^= data[i] &amp; 0xFF;
              for (int j = 0; j &lt; 8; j++)
              {
                  if ((current_crc_value &amp; 1) != 0)
                  {
                      current_crc_value = (current_crc_value &gt;&gt; 1) ^ POLYNOMIAL;
                  }
                  else
                  {
                      current_crc_value = current_crc_value &gt;&gt; 1;
                  }
              }
          }
          current_crc_value = ~current_crc_value;
  
          return current_crc_value &amp; 0xFFFF;
      }
  </code></pre>
  
  <p>this is the another implementation that i used but both gives different values</p>
  
  <pre><code>    const ushort polynomial = 0xA001;
      ushort[] table = new ushort[256];
  
      public ushort ComputeChecksum(byte[] bytes)
      {
          ushort crc = 0;
          for (int i = 0; i &lt; bytes.Length; ++i)
          {
              byte index = (byte)(crc ^ bytes[i]);
              crc = (ushort)((crc &gt;&gt; 8) ^ table[index]);
          }
          return crc;
      }
  
      public byte[] ComputeChecksumBytes(byte[] bytes)
      {
          ushort crc = ComputeChecksum(bytes);
          return BitConverter.GetBytes(crc);
      }
  
      public Crc16()
      {
          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>
  
  <p>The value I`m using is an <strong><em>Octet String</em></strong>  <strong>"[jp3]TEST [fl]Flashing[/fl]"</strong> and its expected value is <strong>95F9</strong> in hex. This is an example on the guide of NTCIP protocol</p>
  
  <p>Thanks</p>