Public Class CRC16

    Public Shared Function NetCRC16(ByRef buffer() As Byte, ByVal offset As UInt32, ByVal len As Integer, ByRef logstr As String) As UInt16
        Dim xda As UInt16
        Dim xdapoly As UInt16
        Dim i As Integer
        Dim j As Integer
        Dim xdabit As UInt16

        xda = &HFFFF
        xdapoly = &HA001
        For i = 0 To (len - 1)
            xda = xda Xor buffer(offset)

            'logstr += Hex(buffer(offset)).PadLeft(2, "0")
            'logstr += " "


            offset += 1
            For j = 0 To 7
                xdabit = CByte(xda And &H1)
                xda = xda \ 2
                If xdabit > 0 Then
                    xda = xda Xor xdapoly
                End If
            Next
        Next
        Return xda
    End Function
End Class
