產生字串校正碼的方法

來源:互聯網
上載者:User

         以前的一個項目中遇到過要核對條碼的校正碼的需求,感覺有時候還是非常有用的,下面提供代碼,代碼版本為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 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.