title: example for VB.NET to calculate CRC16 of an string or Byte Array
body: <p>With reference to this link
  <a href="https://stackoverflow.com/questions/15553697/calculate-crc32-of-an-string-or-byte-array?answertab=votes#tab-top">Calculate CRC32 of an String or Byte Array</a>
  I modified the code in order to calculate the CRC16 instead of CRC32, however I am getting wrong result, can some one point me where is the mistake?</p>
  
  <pre><code>Private Sub Main()
  Crc16.ComputeChecksum(Encoding.UTF8.GetBytes("Some string"))
  End Sub
      Public Class CRC16
  Shared table As UShort()
  
  Shared Sub New()
      Dim poly As UShort = &amp;HA001US 'calculates CRC-16 using A001 polynomial (modbus)
      table = New UShort(255) {}
      Dim temp As UShort = 0
      For i As UShort = 0 To table.Length - 1
          temp = i
          For j As Integer = 8 To 1 Step -1
              If (temp And 1) = 1 Then
                  temp = CUShort((temp &gt;&gt; 1) Xor poly)
              Else
                  temp &gt;&gt;= 1
              End If
          Next
          table(i) = temp
      Next
  End Sub
  
  Public Shared Function ComputeChecksum(ByVal bytes As Byte()) As UShort
      Dim crc As UShort = &amp;H0US ' The calculation start with 0x00
      For i As Integer = 0 To bytes.Length - 1
          Dim index As Byte = CByte(((crc) And &amp;HFF) Xor bytes(i))
          crc = CUShort((crc &gt;&gt; 8) Xor table(index))
      Next
      Return Not crc
  End Function
  End Class
  </code></pre>