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