现在,为了达到要点,我无法调用此方法,因为此方法需要一些字符串值,并且我已经传递了十六进制值。我怎么能这样做。
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
" 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
答案 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