Vb.net 使用多项式0x8408和初始crc 0xFFFF计算CRC16

Vb.net 使用多项式0x8408和初始crc 0xFFFF计算CRC16,vb.net,crc16,Vb.net,Crc16,嗨,我需要得到带有多项式0x8408和初始0xFFFF的CRC16,在这篇文章中我发现了一个相同的情况 但我在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 Di

嗨,我需要得到带有多项式0x8408和初始0xFFFF的CRC16,在这篇文章中我发现了一个相同的情况

但我在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
Private Sub crc16calc\u Click(发送方作为对象,e作为事件参数)处理crc16calc。单击
尺寸数据为UShort=0
尺寸crc为UShort=&HFFFF
Dim pru()作为字节={H5、&H0、&H4、&HFB、&H4A、&H43}
对于j,单位长度=0至实际长度-1
crc=CUShort(crc^pru(j))
对于i As UInteger=0到7
如果((crc&&H1)=1),则
crc=CUShort((crc>>1)^&H8408)
其他的
crc>>=1
如果结束
下一个
下一个
crc=CUShort(非crc)
数据=crc
crc=CUShort((crc>8&&HFF))
MsgBox(crc)
端接头
但是当我执行这段代码时,会在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
Private Sub crc16calc\u Click(发送方作为对象,e作为事件参数)处理crc16calc。单击
作为UShort的Dim数据
尺寸预设值为UShort=&HFFFF
作为UShort=&H8408的Dim多项式
Dim pru()作为字节={&H4、&H0、&H1、&HDB、&H4B}'最后两个字节是CRC16
Dim pru2()作为字节={H5、&H0、&H1、&HFB、&HF2、&H3D}
尺寸ucX为整数=实际长度-3
尺寸UICRC值为UShort=预设值
对于j,整数=0到ucX
uiCrcValue=uiCrcValue Xor pru(j)
对于i,整数=0到7
如果(UICRC值和1),则
uiCrcValue=(uiCrcValue>>1)异或多项式
其他的
uiCrcValue=uiCrcValue>>1
如果结束
下一个
下一个
'MsgBox(uiCrcValue)
数据=UICRC值
uiCrcValue=CUShort((uiCrcValue>8)和&HFF))
MsgBox(UICRC值)
端接头
谢谢,代码在C中运行

,是异或。在VB中,^运算符是求幂运算

只需将每个
^
更改为
Xor