来自字节数组的crc16 ccitt

时间:2016-01-07 10:35:46

标签: vb.net crc16

  1. 我需要一些帮助来为数组创建tcp数据包的校验和 字节。
  2. 这是代码正在运行,因为我已经测试过它 string。
  3. 问题是我要将一个字节数组传递给它 功能。
  4. 此刻我通过努力创造了它 嵌入时间序列号,否则登录的代码很长 到一个设备。
  5. 这个值是b1:68:de:3a:15:cd:5b:07用wireshark检查,CRC应该用在线crc检查E2B6 this
  6. 现在,为了达到要点,我无法调用此方法,因为此方法需要一些字符串值,并且我已经传递了十六进制值。我怎么能这样做。

    Private Sub ConnectTCP_Click(sender As Object, e As EventArgs) Handles ConnectTCP.Click
    Dim Serial As UInt32 = "123456789"
    Dim time As UInt32 = "987654321"
    Dim buffer2() As Byte = BitConverter.GetBytes(time)
    Dim buffer3() As Byte = BitConverter.GetBytes(Serial)
    Dim array4(buffer2.Length + buffer3.Length - 1) As Byte
    Array.Copy(buffer2, array4, buffer2.Length)
    Array.Copy(buffer3, 0, array4, buffer2.Length, buffer3.Length)
    getCRC16(array4) 'What I need to do here
           end sub
    
  7. " CRC16 CCITT功能""

    Public Function getCRC16(ByVal strInput As String)
        Dim lngCheck As Long
        Dim Power(7) As Integer
        Dim I As Integer
        Dim J As Integer
        Dim Poly As Long
        Dim CRC As Long
        Dim TestBit As Boolean
        Dim TestBit1 As Boolean
        Dim TestBit2 As Boolean
        Poly = &H1021
        CRC = &HFFFF
        For J = 0 To 7
            Power(J) = 2 ^ J
        Next J
        For I = 1 To Len(strInput)
            lngCheck = Asc(Mid$(strInput, I, 1))
            For J = 7 To 0 Step -1
                If (CRC And 32768) = 32768 Then
                    TestBit1 = True
                Else
                    TestBit1 = False
                End If
                If (lngCheck And Power(J)) = Power(J) Then
                    TestBit2 = True
                Else
                    TestBit2 = False
                End If
                TestBit = TestBit1 Xor TestBit2
                CRC = (CRC And 32767) * 2
                If TestBit = True Then
                    CRC = CRC Xor Poly
                End If
            Next J
        Next I
        Dim tmp As String
        tmp = Hex(CRC)
        CRCTCP.Text = tmp
        getCRC16 = tmp
    End Function
    

1 个答案:

答案 0 :(得分:0)

最后,发现它。

Public Class CRC16CCITT
    Public Enum InitialCRCValue
        Zeroes = 0
        NonZero1 = &HFFFF
        NonZero2 = &H1D0F
        'NonZero3 = &H0
    End Enum

    Private Const poly As UShort = &H1021 'polynomial
    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
        'Dim x As String
        For i As Integer = 0 To bytes.Length - 1
            crc = CUShort(((crc << 8) Xor table(((crc >> 8) Xor (&HFF And bytes(i))))))
            'crc = (crc << 8) ^ (x << 15) ^ (x << 2) ^ x
        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