校验位计算
字母表中的数字和字母被赋值。0 - 9 是 0 - 9。A - Z 是 10 - 35,空格是 36。
示例字符串:CA5B9AJ1564...
计算:
C = 12
A = 10
5 = 5
B = 11
9 = 9
A = 10
J = 19
1 = 1
5 = 5
6 = 6
4 = 4
每个 Value 从 36 到 1 以降序相乘,如下:
36 * 12 = 432
35 * 10 = 350
34 * 5 = 170
33 * 11 = 363
32 * 9 = 288
31 * 10 = 310
30 * 19 = 570
29 * 1 = 29
28 * 5 = 140
27 * 6 = 162
26 * 4 = 104
=2918
2918 然后除以 37 = 78 R 32
从被除数中减去 32 的余数 = 37 - 32 =
5 是校验位
我需要帮助将上述算法转换为 vb.net 中的一系列嵌套循环或私有子程序。我想使用 Mod 运算符,因为我们现在在公司使用的公式减少了 3。我在 Excel 中手动运行了 3 个不同的字符串。
当前代码:(使用测试字符串关闭 3)
Private Function CheckDigit37(ByVal FieldString As String)
Dim String36Value As Integer
Dim PositionIndex As Integer
Dim LastPosition As Integer
Dim CharIndex As Integer
Dim CharString As String
Dim Value37 As Long
Dim Remainder As Long
Dim CheckDigitValue As Integer
Dim CheckDigit As String = " "
If FieldString = "" Then
Return False
Else
FieldString = FieldString.ToUpper
String36Value = RoundUp(FieldString.Length / 36)
End If
For PositionIndex = 1 To String36Value
If FieldString.Length = 36 Then
LastPosition = 36
ElseIf PositionIndex < String36Value Then
LastPosition = 36
Else
LastPosition = FieldString.Length - 36 * (PositionIndex - 1)
End If
For CharIndex = 1 To LastPosition
CharString = Mid(FieldString, CharIndex + (PositionIndex - 1) * 36, 1)
'If CharString = "" Then GoTo EndLoop
Select Case CharString
Case 0
Value37 = Value37 + 0 * (37 - CharIndex)
Case 1
Value37 = Value37 + 1 * (37 - CharIndex)
Case 2
Value37 = Value37 + 2 * (37 - CharIndex)
Case 3
Value37 = Value37 + 3 * (37 - CharIndex)
Case 4
Value37 = Value37 + 4 * (37 - CharIndex)
Case 5
Value37 = Value37 + 5 * (37 - CharIndex)
Case 6
Value37 = Value37 + 6 * (37 - CharIndex)
Case 7
Value37 = Value37 + 7 * (37 - CharIndex)
Case 8
Value37 = Value37 + 8 * (37 - CharIndex)
Case 9
Value37 = Value37 + 9 * (37 - CharIndex)
Case "A"
Value37 = Value37 + 10 * (37 - CharIndex)
Case "B"
Value37 = Value37 + 11 * (37 - CharIndex)
Case "C"
Value37 = Value37 + 12 * (37 - CharIndex)
Case "D"
Value37 = Value37 + 13 * (37 - CharIndex)
Case "E"
Value37 = Value37 + 14 * (37 - CharIndex)
Case "F"
Value37 = Value37 + 15 * (37 - CharIndex)
Case "G"
Value37 = Value37 + 16 * (37 - CharIndex)
Case "H"
Value37 = Value37 + 17 * (37 - CharIndex)
Case "I"
Value37 = Value37 + 18 * (37 - CharIndex)
Case "J"
Value37 = Value37 + 19 * (37 - CharIndex)
Case "K"
Value37 = Value37 + 20 * (37 - CharIndex)
Case "L"
Value37 = Value37 + 21 * (37 - CharIndex)
Case "M"
Value37 = Value37 + 22 * (37 - CharIndex)
Case "N"
Value37 = Value37 + 23 * (37 - CharIndex)
Case "O"
Value37 = Value37 + 24 * (37 - CharIndex)
Case "P"
Value37 = Value37 + 25 * (37 - CharIndex)
Case "Q"
Value37 = Value37 + 26 * (37 - CharIndex)
Case "R"
Value37 = Value37 + 27 * (37 - CharIndex)
Case "S"
Value37 = Value37 + 28 * (37 - CharIndex)
Case "T"
Value37 = Value37 + 29 * (37 - CharIndex)
Case "U"
Value37 = Value37 + 30 * (37 - CharIndex)
Case "V"
Value37 = Value37 + 31 * (37 - CharIndex)
Case "W"
Value37 = Value37 + 32 * (37 - CharIndex)
Case "X"
Value37 = Value37 + 33 * (37 - CharIndex)
Case "Y"
Value37 = Value37 + 34 * (37 - CharIndex)
Case "Z"
Value37 = Value37 + 35 * (37 - CharIndex)
Case " "
Value37 = Value37 + 36 * (37 - CharIndex)
Case Else
Value37 = Value37 + 0 * (37 - CharIndex)
End Select
Next
Next
Remainder = Value37 - Int(Value37 / 37) * 37
CheckDigitValue = 37 - Remainder
Select Case CheckDigitValue
Case 0
CheckDigit = 0
Case 1
CheckDigit = 1
Case 2
CheckDigit = 2
Case 3
CheckDigit = 3
Case 4
CheckDigit = 4
Case 5
CheckDigit = 5
Case 6
CheckDigit = 6
Case 7
CheckDigit = 7
Case 8
CheckDigit = 8
Case 9
CheckDigit = 9
Case "10"
CheckDigit = "A"
Case "11"
CheckDigit = "B"
Case "12"
CheckDigit = "C"
Case "13"
CheckDigit = "D"
Case "14"
CheckDigit = "E"
Case "15"
CheckDigit = "F"
Case "16"
CheckDigit = "G"
Case "17"
CheckDigit = "H"
Case "18"
CheckDigit = "I"
Case "19"
CheckDigit = "J"
Case "20"
CheckDigit = "K"
Case "21"
CheckDigit = "L"
Case "22"
CheckDigit = "M"
Case "23"
CheckDigit = "N"
Case "24"
CheckDigit = "O"
Case "25"
CheckDigit = "P"
Case "26"
CheckDigit = "Q"
Case "27"
CheckDigit = "R"
Case "28"
CheckDigit = "S"
Case "29"
CheckDigit = "T"
Case "30"
CheckDigit = "U"
Case "31"
CheckDigit = "V"
Case "32"
CheckDigit = "W"
Case "33"
CheckDigit = "X"
Case "34"
CheckDigit = "Y"
Case "35"
CheckDigit = "Z"
Case "36"
CheckDigit = " "
Case "37"
CheckDigit = "0"
End Select
Return CheckDigit
End Function