-1

チェックディジットの計算

アルファベットの数字と文字は割り当てられた値です。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
4

1 に答える 1

0

あなたの質問とあなたのコードを正しく読むと、指定された文字列のチェック文字列値を生成したいように思えます (つまり、1 の場合、チェック値は 1、B の場合、チェック値は 11 です)。あなたが提出したコードがコンパイルされるかどうかは完全にはわかりませんが (私は試していません)、ASCII テーブルと 関数ChrAsc関数を使用することで、このコードを大幅に削減できます。

0 ~ 9 の ASCII 値は 48 ~ 55 で、A ~ Z の ASCII 値は 65 ~ 90 です。最初のセットから 48 を引き、2 番目のセットから 55 を引くと、アルゴリズムが適用する値と一致します。

一連の丸めや文字列操作を行うよりも、一度に 1 文字ずつ文字列をループし、必要な操作を行ってから、結果をチェック ディジットの対応する値に変換する方が簡単だと思います。このようなもの:

Private Function CheckDigit37(ByVal FieldString As String) As String

    Dim CharIndex As Integer = 36
    Dim Value37 As Long
    Dim CheckDigitValue As Integer
    Dim CheckDigit As String = ""
    ' This will hold the character we're currently evaluating
    Dim C As Char

    FieldString = FieldString.ToUpper()

    For i As Integer = 0 To FieldString.Length - 1

        ' If CharIndex has reached 0 and we still have characters left,
        ' change it back to 36
        If (CharIndex = 0) Then
            CharIndex = 36
        End If

        ' Get the next character in the string
        C = FieldString(i)

        If (IsNumeric(C)) Then
            ' Subtract 48 from the ASCII value to get the algorithm value
            Value37 = Value37 + (CharIndex * (Asc(C) - 48))
        ElseIf (Asc(C) >= 65 AndAlso Asc(C) <= 90) Then
            ' Subtract 55 from the ASCII value to get the algorithm value
            Value37 = Value37 + (CharIndex * (Asc(C) - 55))
        ElseIf (Asc(C) = 32) Then
            ' We have a space (ASCII 32)
            Value37 = Value37 + (CharIndex * 36)
        End If

        ' Decrease the CharIndex by 1
        CharIndex = CharIndex - 1
    Next

    CheckDigitValue = 37 - (Value37 Mod 37)

    If (CheckDigitValue <= 9) Then
        ' We have a number
        CheckDigit = Chr(CheckDigitValue + 48)
    ElseIf (CheckDigitValue = 36) Then
        ' We have a space
        CheckDigit = Chr(32)
    Else
        ' We have A - Z
        CheckDigit = Chr(CheckDigitValue + 55)
    End If

    Return CheckDigit
End Function

これにより、例の値は 5 になります。

また、より長い文字列「CA5B9AJ15642AKUQ02DA023MSAEWDSE924ADSCAA21」を使用し、CheckDigitValue が 21 で、CheckDigit が「L」でした。

于 2013-04-23T04:47:10.907 に答える