-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

每个 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
4

1 回答 1

0

如果我正确阅读了您的问题和代码,听起来您想为给定字符串生成检查字符串值(即,如果为 1,则检查值为 1,如果为 B,则检查值为 11)。我不完全确定您提出的代码会编译(我没有尝试过),但是通过使用 ASCII 表和ChrandAsc函数,您可以大大减少此代码。

0 - 9 的 ASCII 值是 48 - 55,A - Z 的 ASCII 值是 65 - 90。从第一组中减去 48 并从第二组中减去 55 将匹配您的算法应用的值。

与其做一堆舍入和字符串操作,我认为一次循环一个字符,做必要的操作,然后将结果转换为校验位的相应值会更容易。像这样的东西:

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 的值。

我还使用了更长的字符串“CA5B9AJ15642ACKUQ02DA023MSAEWDSE924ADSCAA21”,得到的 CheckDigitValue 为 21,CheckDigit 为“L”。

于 2013-04-23T04:47:10.907 回答