生成字符串校验码的方法

前端之家收集整理的这篇文章主要介绍了生成字符串校验码的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

以前的一个项目中遇到过要核对条码的校验码的需求,感觉有时候还是非常有用的,下面提供代码代码版本为VB.net:

//根据输入的的条码来计算该条码的校验位

Private Function GetCheckDigit(Byal strCode As String) As String
Dim intCurVal As Int16 = 0
Dim intChksum As Int16 = 0
Dim chrCur As String
Dim intPos As Int16
'Check for invalid characters and calculate check sum
For intPos = 1 To Len(strCode)
chrCur = Mid(strCode,intPos,1)
Select Case chrCur
Case "0" To "9"
intCurVal = Val(chrCur)
Case "A" To "Z"
intCurVal = Asc(chrCur) - 55
Case "-"
intCurVal = 36
Case "."
intCurVal = 37
Case " "
intCurVal = 38
Case "$"
intCurVal = 39
Case "/"
intCurVal = 40
Case "+"
intCurVal = 41
Case "%"
intCurVal = 42
Case Else
End Select
intChksum = intChksum + intCurVal
Next
intChksum = intChksum Mod 43
Select Case intChksum
Case 0 To 9
Return intChksum.ToString()
Case 10 To 35
Return Chr(intChksum + 55)
Case 36
Return "-"
Case 37
Return "."
Case 38
Return " "
Case 39
Return "$"
Case 40
Return "/"
Case 41
Return "+"
Case 42
Return "%"
Case Else
End Select
Return "0" End Function
原文链接:https://www.f2er.com/vb/260836.html

猜你在找的VB相关文章