计算法就是依据CRC校验码的产生原理来设计程序。其优点是模块代码少,修改灵活,可移植性好。其缺点为计算量大。为了便于理解,这里假定了三位数据,而多项式码为A001(hex)。
成都创新互联公司专注于博山企业网站建设,成都响应式网站建设,商城网站定制开发。博山网站建设公司,为博山等地区提供建站服务。全流程按需设计,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务
在窗体上放置一命令按钮Command1,并添加如下代码:
Private Sub Command1_Click()
Dim CRC() As Byte
Dim d() As Byte '待传输数据
ReDim d(2) As Byte
d(0) = 123
d(1) = 112
d(2) = 135
CRC = CRC16(d) '调用CRC16计算函数
'CRC(0)为高位
'CRC(1)为低位
End Sub
注意:在数据传输时CRC的低位可能在前,而高位在后。
大概看了下。有变量定义类型错误,修改如下:
Public
Function
crc16(ByRef
cmdstring()
As
Byte,
DataLen
As
Integer)
As
String
Dim
data
As
Integer
Dim
i
As
Integer
Dim
CRCHi
As
long,
CRCLo
As
long'这里应该定义为long.因为下面赋值是long型。朋友。
Dim
iIndex
As
Long
Dim
CRCStr
As
String
Dim
DataStr
As
String
CRCLo
=
HFF'看这里的赋值。long型
CRCHi
=
HFF
For
i
=
To
DataLen
iIndex
=
CRCLo
Xor
cmdstring(i)
CRCLo
=
CRCHi
Xor
GetCRCLo(iIndex)
'低位处理
CRCHi
=
GetCRCHi(iIndex)
'高位处理
DataStr
=
DataStr
Chr(cmdstring(i))
Next
i
Dim
ReturnData(1)
As
Byte
ReturnData(1)
=
CRCHi
ReturnData(0)
=
CRCLo
CRCStr
=
StrConv(ReturnData,
vbUnicode)
crc16
=
DataStr
+
CRCStr
End
Function
就是收集boot.ini的不变的一些相关的信息(如文件大小,文件名等等),然后组成字符串,再用md5函数把组成的字符串编码。
以后就用这个编码进行校验了,字符串比较(compare即可)。
不过对boot.ini文件校验没有多大的意义,随便装一个什么dos工具箱或者用优化软件优化以下那个校验码准变。
Private Sub Form_Load()
'text1为校验对象
Text1.Text = "31303432"
'text2为校验多项式
Text2.Text = "180D"
End Sub
Private Sub Command1_Click()
Text3.Text = CRC(Text1.Text, Text2.Text, 12)
End Sub
Private Function CRC(ByVal Hex_Number As String, ByVal Hex_CRC As String, Optional ByVal C As Integer = 16) As String
'Hex_Number为校验项16进制数字,Hex_CRC为校验多项式,16进制,C为默认位数CRC-16
Dim i As Integer
Dim BinNumber As String
Dim BinCRC As String
Dim Temp As String
CRC = "0"
If Hex_Number = "0" Then Exit Function
BinNumber = HEXtoBin(Hex_Number)
BinCRC = HEXtoBin(Hex_CRC)
i = Len(BinCRC)
BinNumber = BinNumber String(C, "0")
CRC = Left(BinNumber, i)
Temp = Right(BinNumber, Len(BinNumber) - i)
Temp = HEXtoBin(Hex(CLng("H" BINtoHEX(CRC)) Xor CLng("H" Hex_CRC))) Temp
CRC = BINtoHEX(Temp)
If Len(Temp) = i Then
CRC = CRC(CRC, Hex_CRC, 0)
End If
End Function
Private Function HEXtoBin(Hex_Number) As String
'十六进制转换为二进制
Dim i As Long
Dim B As String
Hex_Number = UCase(Hex_Number)
For i = 1 To Len(Hex_Number)
Select Case Mid(Hex_Number, i, 1)
Case "0": B = B "0000"
Case "1": B = B "0001"
Case "2": B = B "0010"
Case "3": B = B "0011"
Case "4": B = B "0100"
Case "5": B = B "0101"
Case "6": B = B "0110"
Case "7": B = B "0111"
Case "8": B = B "1000"
Case "9": B = B "1001"
Case "A": B = B "1010"
Case "B": B = B "1011"
Case "C": B = B "1100"
Case "D": B = B "1101"
Case "E": B = B "1110"
Case "F": B = B "1111"
End Select
Next i
While Left(B, 1) = "0"
B = Right(B, Len(B) - 1)
Wend
HEXtoBin = B
End Function
Private Function BINtoHEX(ByVal Bin_Number As String) As String
'二进制转换为十六进制
Dim i As Long
Dim H As String
If Len(Bin_Number) Mod 4 0 Then
Bin_Number = String(4 - Len(Bin_Number) Mod 4, "0") Bin_Number
End If
For i = 1 To Len(Bin_Number) Step 4
Select Case Mid(Bin_Number, i, 4)
Case "0000": H = H "0"
Case "0001": H = H "1"
Case "0010": H = H "2"
Case "0011": H = H "3"
Case "0100": H = H "4"
Case "0101": H = H "5"
Case "0110": H = H "6"
Case "0111": H = H "7"
Case "1000": H = H "8"
Case "1001": H = H "9"
Case "1010": H = H "A"
Case "1011": H = H "B"
Case "1100": H = H "C"
Case "1101": H = H "D"
Case "1110": H = H "E"
Case "1111": H = H "F"
End Select
Next i
While Left(H, 1) = "0"
H = Right(H, Len(H) - 1)
Wend
BINtoHEX = H
End Function