'
' CRC16CCIT.ComputeCheckSum will return ushort with CRC16
' CRC16CCIT.ComputeCheckSumBytes will return 2 bytes (hi and low)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim checksumcalculator = New CRC16CCITT(CRC16CCITT.InitialCRCValue.NonZero1)
Dim value() As Byte = System.Text.Encoding.ASCII.GetBytes("123456789")
MsgBox(Format(checksumcalculator.ComputeCheckSum(value), "X2")) ' 29B1
End Sub
Public Class CRC16CCITT
Public Enum InitialCRCValue
Zeroes = 0
NonZero1 = &HFFFF
NonZero2 = &H1D0F
End Enum
Private Const poly As UShort = &H1021
Dim table(255) As UShort
Dim intValue As UShort = 0
Public Function ComputeCheckSum(ByVal bytes As Byte()) As UShort
Dim crc As UShort = Me.intValue
For i As Integer = 0 To bytes.Length - 1
crc = CUShort(((crc << 8) Xor table(((crc >> 8) Xor (&HFF And bytes(i))))))
Next
Return crc
End Function
Public Function ComputeCheckSumBytes(ByVal bytes As Byte()) As Byte()
Dim crc As UShort = ComputeCheckSum(bytes)
Return BitConverter.GetBytes(crc)
End Function
Public Sub New(ByVal initialvalue As InitialCRCValue)
Me.intValue = CUShort(initialvalue)
Dim temp, a As UShort
For i As Integer = 0 To table.Length - 1
temp = 0
a = CUShort(i << 8)
For j As Integer = 0 To 7
If ((temp Xor a) And &H8000) <> 0 Then
temp = CUShort((temp << 1) Xor poly)
Else
temp <<= 1
End If
a <<= 1
Next
table(i) = temp
Next
End Sub
End Class