チェックディジットの計算
アルファベットの数字と文字は割り当てられた値です。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
各値は、次のように
36から
1まで降順で乗算
さ
れ
ます
。
29 * 1 = 29
28 * 5 = 140
27 * 6 = 162
26 * 4 = 104
=2918
次に、2918 を 37 で割る = 78 R 32
剰余の 32 を被除数から引く = 37 - 32 =
5 がチェック ディジット
上記のアルゴリズムを一連のネストされたループまたは vb.net のプライベート サブルーチンに変換するための支援が必要です。会社で現在使用している数式が 3 ずれているため、Mod 演算子を使用したいと思います。これを 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