title: Calcule the CRC16 whit POLYNOMIAL 0x8408 and initial crc 0xFFFF
body: <p>Hi I need get the CRC16 with polynomial 0x8408 and initial 0xFFFF, in this post I found a equal case </p>
  
  <p><a href="https://stackoverflow.com/questions/30496223/c-sharp-crc-16-ccitt-0x8408-polynomial-help-needed">C# CRC-16-CCITT 0x8408 Polynomial. Help needed</a></p>
  
  <p>but I am working in VB 2013 and I intend write the same code but in this language</p>
  
  <pre><code>Private Sub crc16calc_Click(sender As Object, e As EventArgs) Handles crc16calc.Click
  
          Dim data As UShort = 0
          Dim crc As UShort = &amp;HFFFF
          Dim pru() As Byte = {&amp;H5, &amp;H0, &amp;H4, &amp;HFB, &amp;H4A, &amp;H43}
  
          For j As UInteger = 0 To pru.Length - 1
              crc = CUShort(crc ^ pru(j))
              For i As UInteger = 0 To 7
                  If ((crc &amp; &amp;H1) = 1) Then
                      crc = CUShort((crc &gt;&gt; 1) ^ &amp;H8408)
                  Else
                      crc &gt;&gt;= 1
                  End If
  
              Next
  
          Next
  
  
          crc = CUShort(Not crc)
          data = crc
          crc = CUShort((crc &lt;&lt; 8) ^ (data &gt;&gt; 8 &amp; &amp;HFF))
  
          MsgBox(crc)
  
      End Sub
  </code></pre>
  
  <p>But when I execute this code get a overflow in crc = CUShort(crc ^ pru(j))</p>
  
  <p>Can any help me.</p>
  
  <pre><code>Private Sub crc16calc_Click(sender As Object, e As EventArgs) Handles crc16calc.Click
  
      Dim data As UShort
      Dim PRESET_VALUE As UShort = &amp;HFFFF
      Dim POLYNOMIAL As UShort = &amp;H8408
      Dim pru() As Byte = {&amp;H4, &amp;H0, &amp;H1, &amp;HDB, &amp;H4B} ' the two last bytes are the CRC16
      Dim pru2() As Byte = {&amp;H5, &amp;H0, &amp;H1, &amp;HFB, &amp;HF2, &amp;H3D}
      Dim ucX As Integer = pru.Length - 3
  
      Dim uiCrcValue As UShort = PRESET_VALUE
  
      For j As Integer = 0 To ucX
  
          uiCrcValue = uiCrcValue Xor pru(j)
  
          For i As Integer = 0 To 7
  
              If (uiCrcValue And 1) Then
                  uiCrcValue = (uiCrcValue &gt;&gt; 1) Xor POLYNOMIAL
              Else
                  uiCrcValue = uiCrcValue &gt;&gt; 1
              End If
  
          Next
  
      Next
  
      'MsgBox(uiCrcValue)
      data = uiCrcValue
      uiCrcValue = CUShort((uiCrcValue &lt;&lt; 8) Xor ((data &gt;&gt; 8) And &amp;HFF))
      MsgBox(uiCrcValue)
  
  End Sub
  </code></pre>
  
  <p>Thanks, the code is working</p>