Click here to Skip to main content
15,891,375 members
Articles / Programming Languages / Visual Basic

How to Generate Check Digits and verify your barcodes

Rate me:
Please Sign up or sign in to vote.
4.63/5 (13 votes)
28 Sep 2006CPOL 83.2K   993   44  
Generates Check Digits and alows you to verify proper scan of a barcode
'this only does 12 digit barcodes for now...
'if your interested in how this works go to
'www.howstuffworks.com/upc.htm
'thats where i got most of my information...just thought it
'would be nice to have a quick funcion to do the math for me
'and figured i should share ;)
Public Class Barcode
    Public Shared Function VerifyBarcodeInfo(ByVal info As String) As Boolean
        Return ((((((Val(info.Chars(0)) + Val(info.Chars(2)) + Val(info.Chars(4)) + Val(info.Chars(6)) + _
                Val(info.Chars(8)) + Val(info.Chars(10))) * 3) + (Val(info.Chars(1)) + Val(info.Chars(3)) + _
                Val(info.Chars(5)) + Val(info.Chars(7)) + Val(info.Chars(9)))) + (Val(info.Chars(11)))) _
                Mod 10) = 0)
    End Function

    Public Shared Function GenerateCheckDigit(ByVal theCode As String) As String
        If theCode.Length = 11 Then
            Return theCode & (10 - ((((Val(theCode.Chars(0)) + Val(theCode.Chars(2)) + Val(theCode.Chars(4)) + Val(theCode.Chars(6)) + Val(theCode.Chars(8)) + Val(theCode.Chars(10))) * 3) + (Val(theCode.Chars(1)) + Val(theCode.Chars(3)) + Val(theCode.Chars(5)) + Val(theCode.Chars(7)) + Val(theCode.Chars(9)))) Mod 10)).ToString
        End If
    End Function
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) www.ruskin.com
United States United States
PC Programmer/Analyst

Comments and Discussions