以前的一个项目中遇到过要核对条码的校验码的需求,感觉有时候还是非常有用的,下面提供代码,代码版本为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