Attribute VB_Name = "CRC"
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'用来计算CRC值的子程序
'BCRC是所要计算的进制值(16或32),如16位CRC时,其值即为16
'GP是生成多项式,以16进制表示较佳
'BDF是欲求出CRC的原始数值
'返回值即是计算后的CRC值
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CalCRC(BCRC As Integer, GP As Long, BDF As Long) As Long
Dim C1&, C2&
Dim A1%, PreC1&
Dim LeftValue%, A1Shift%
If BDF = 0 Then
CalCRC = 0
Exit Function
End If
A1 = (Len(Hex(BDF)) - 1) * 4 '求出第一次的参考偏移位元值
C1 = BDF * 2 ^ BCRC '将被求值先乘上位值
LeftValue = Val("&H" & Mid(Hex(BDF), 1, 1)) '取出最左边的值
If LeftValue >= 1 Then A1Shift = 1 '只有一位数,权值为1
If LeftValue >= 2 Then A1Shift = 2 '只有二位数,权值为2
If LeftValue >= 4 Then A1Shift = 3 '只有三位数,权值为3
If LeftValue >= 8 Then A1Shift = 4 '四位数,权值为4
A1 = A1 + (A1Shift - 1) '第一次的偏移值
C2 = GP * 2 ^ A1 '第一次的运算值
Do
PreC1 = C1 '记录先前值
C1 = C1 Xor C2 '二进制长除法之余数
Do
A1 = A1 - 1 '''
If C1 > PreC1 Then '若余数大于生成多项式,则予以降序
C1 = PreC1 Xor (GP * 2 ^ A1)
Else
Exit Do
End If
Loop
C2 = GP * 2 ^ A1 '下一次的运算值
Loop Until C1 CalCRC = C1
End Function