嗨,我需要得到多项式0x8408和初始值0xFFFF的CRC16,在这篇文章中我找到了一个相同的情况。
C# CRC-16-CCITT 0x8408多项式。需要帮助
但我在VB 2013中工作,我打算用这种语言编写同样的代码
Private Sub crc16calc_Click(sender As Object, e As EventArgs) Handles crc16calc.Click
Dim data As UShort = 0
Dim crc As UShort = &HFFFF
Dim pru() As Byte = {&H5, &H0, &H4, &HFB, &H4A, &H43}
For j As UInteger = 0 To pru.Length - 1
crc = CUShort(crc ^ pru(j))
For i As UInteger = 0 To 7
If ((crc & &H1) = 1) Then
crc = CUShort((crc >> 1) ^ &H8408)
Else
crc >>= 1
End If
Next
Next
crc = CUShort(Not crc)
data = crc
crc = CUShort((crc << 8) ^ (data >> 8 & &HFF))
MsgBox(crc)
End Sub但是,当我执行此代码时,crc = CUShort(crc ^ pru(j))中会出现溢出。
有谁能帮我吗。
Private Sub crc16calc_Click(sender As Object, e As EventArgs) Handles crc16calc.Click
Dim data As UShort
Dim PRESET_VALUE As UShort = &HFFFF
Dim POLYNOMIAL As UShort = &H8408
Dim pru() As Byte = {&H4, &H0, &H1, &HDB, &H4B} ' the two last bytes are the CRC16
Dim pru2() As Byte = {&H5, &H0, &H1, &HFB, &HF2, &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 >> 1) Xor POLYNOMIAL
Else
uiCrcValue = uiCrcValue >> 1
End If
Next
Next
'MsgBox(uiCrcValue)
data = uiCrcValue
uiCrcValue = CUShort((uiCrcValue << 8) Xor ((data >> 8) And &HFF))
MsgBox(uiCrcValue)
End Sub谢谢,代码正在运行
发布于 2015-09-16 19:45:03
在C#中,^算子是xor。在VB中,^运算符是幂运算。
只需将每个^更改为Xor即可。
https://stackoverflow.com/questions/32616770
复制相似问题