vb如何实现CRC12校验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 = BinNumberString(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 40 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
vb 如何校验文件的CRC 或者MD5或什么都行就是收集boot.ini的不变的一些相关的信息(如文件大?。?文件名等等),然后组成字符串,再用md5函数把组成的字符串编码 。
以后就用这个编码进行校验了 , 字符串比较(compare即可) 。
不过对boot.ini文件校验没有多大的意义,随便装一个什么dos工具箱或者用优化软件优化以下那个校验码准变 。
vb,crc校验的程序'Text1.Text 输入数据
'Text2.Text 输出数据
Dim ReturnData(1) As Byte
Private Sub Command1_Click()
Dim CRC(2) As Byte
Dim d(6) As Byte '待传输数据
ReDim data(7) As Byte
For i = 0 To 5
data(i) = "h"Mid(Text1.Text, 2 * i1, 2)
Debug.Print data(i)
Next i
Call CRC16(data, UBound(data) - 2, data(6), data(7)) '调用CRC16计算函数
'CRC(0)为高位
'CRC(1)为低位
End Sub
Function CRC16(data() As Byte, i As Integer, ByRef ReturnDatalo As Byte, ByRef ReturnDatahi As Byte) As String
Dim CRC16Lo As Byte, CRC16Hi As Byte'CRC寄存器
Dim CL As Byte, CH As Byte'多项式码HA001
Dim SaveHi As Byte, SaveLo As Byte
Dim Flag As Integer
CRC16Lo = HFF
CRC16Hi = HFF
CL = H1
CH = HA0
For i = 0 To UBound(data) - 2
CRC16Lo = CRC16Lo Xor data(i) '每一个数据与CRC寄存器进行异或
For Flag = 0 To 9
SaveHi = CRC16Hi
SaveLo = CRC16Lo
CRC16Hi = CRC16Hi \ 2'高位右移一位
CRC16Lo = CRC16Lo \ 2'低位右移一位
If ((SaveHi And H1) = H1) Then '如果高位字节最后一位为1
CRC16Lo = CRC16Lo Or H80'则低位字节右移后前面补1
End If'否则自动补0
If ((SaveLo And H1) = H1) Then '如果LSB为1,则与多项式码进行异或
CRC16Hi = CRC16Hi Xor CH
CRC16Lo = CRC16Lo Xor CL
End If
Next Flag
Next i
ReturnDatahi = CRC16Hi'CRC高位
ReturnDatalo = CRC16Lo'CRC低位
Debug.Print Hex(CRC16Lo), Hex(CRC16Hi)
Text2.Text = Text1.TextHex(CRC16Lo)Hex(CRC16Hi)
End Function
VB 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
vb crc冗余校验计算法就是依据CRC校验码vb.netcrc检验的产生原理来设计程序 。其优点是模块代码少 , 修改灵活,可移植性好 。其缺点为计算量大 。为vb.netcrc检验了便于理解,这里假定了三位数据,而多项式码为A001(hex) 。
在窗体上放置一命令按钮Command1 , 并添加如下代码vb.netcrc检验:
PrivateSubCommand1_Click()
DimCRC()AsByte
Dimd()AsByte'待传输数据
ReDimd(2)AsByte
d(0)=123
d(1)=112
d(2)=135
CRC=CRC16(d)'调用CRC16计算函数
'CRC(0)为高位
'CRC(1)为低位
EndSub
注意:在数据传输时CRC的低位可能在前,而高位在后 。
【vb.netcrc检验 vbv验证】vb.netcrc检验的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于vbv验证、vb.netcrc检验的信息别忘了在本站进行查找喔 。
推荐阅读
- gis是哪个系,gis指的是什么意思
- 圣域电商如何加盟,圣域电商如何加盟的
- vb.net读取doc文件,vbnet读取excel表格
- 包含python画四阶函数的词条
- sqlserver库管灰色,sql数据库为灰色
- mysql两时间比较,mysql两个时间比较
- eclipse运行Java爬虫,eclipse怎么跑java
- php怎么注册数据库 php怎么注册数据库账号
- 口袋妖怪go怎么改语言,口袋妖怪go怎么改语言模式