Calculate checksum for Ean13 barcode number

Peter Nordberg
Peter Nordberg used Ask the Experts™
on
Hi experts,

I would like to calculate the checksum of a Ean13 barcode in my webapplication. I'm not sure how to accomplish this in code, but I have the instructions to do so:

Exempel: EAN13

7 3 0 0 0 1 1 0 0 0 0 1K

1.Sum every second number from right:. 1+0+0+1+0+3=5
2. Multiply the sum with the number 3.    5 X 3 = 15
3.Sum every second of the remaining numbers. 0+0+1+0+0+7=8
4.Add the result to the already calculated sum. 15+8=23
5.The totalsum is subtracted from the nearest ten, in this case 30. Checksum is 30-23=7 and Ean-13:

7 3 0 0 0 1 1 0 0 0 0 1  7

If anyone have a suggestion on how to accomplish this in code (preferably vb.net), it would be great.

Thanks for your help!

Peter
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
Peter NordbergIT Manager

Author

Commented:
Hi and thanks for your code, but it doesn't seem to calculate the right checksum number.

Peter
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
   Function ChecksumEAN13(ByVal barcode As String) As Integer
        Dim multiplier As Integer
        Dim total As Integer
        total = 0
        multiplier = 3
        For Each el As Char In barcode.Reverse()
            If el >= "0" And el <= "9" Then
                total = total + Val(el) * multiplier
                If multiplier = 3 Then multiplier = 1 Else multiplier = 3
            End If
        Next
        ChecksumEAN13 = 10 - (total Mod 10)
    End Function
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

Peter NordbergIT Manager

Author

Commented:
Hi cyberkiwi,

I can't get barcode.Reverse() to work. Do I need to import some namespace or?

Peter
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
What version of VS?

 Function ChecksumEAN13(ByVal barcode As String) As Integer
        Dim multiplier As Integer
        Dim total As Integer
        total = 0
        multiplier = 3
        barcode = CStr(Array.Reverse(barcode.ToCharArray()))
        For Each el As Char In barcode
... rest
Peter NordbergIT Manager

Author

Commented:
VS 2008
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
And you are using VB.Net right?
That is weird - I built and tested it in vb.net/vs2008!
Peter NordbergIT Manager

Author

Commented:
Yes, vb.net. VS 2008
Peter NordbergIT Manager

Author

Commented:
When I use your latest code I get the error message on this row:
barcode = CStr(Array.Reverse(barcode.ToCharArray()))

It says that the expression doesn't produce a value.

Peter
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
I created a new project, dropped a button on it, and this is all I have.
Public Class Form1

    Function ChecksumEAN13(ByVal barcode As String) As Integer
        Dim multiplier As Integer
        Dim total As Integer
        total = 0
        multiplier = 3
        Dim carr() As Char
        carr = barcode.ToCharArray()
        Array.Reverse(carr)
        barcode = CStr(carr)
        For Each el As Char In barcode
            If el >= "0" And el <= "9" Then
                total = total + Val(el) * multiplier
                If multiplier = 3 Then multiplier = 1 Else multiplier = 3
            End If
        Next
        ChecksumEAN13 = 10 - (total Mod 10)
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(ChecksumEAN13("7 3 0 0 0 1 1 0 0 0 0 1K "))
        MsgBox(ChecksumEAN13(""))
    End Sub
End Class

Open in new window

Peter NordbergIT Manager

Author

Commented:
Thanks!

That worked perfectly!

Peter
Peter NordbergIT Manager

Author

Commented:
Hi cyberkiwi,

Just want to ask a question about the function above. It works perfectly exept when the chechsum is supposed to be zero, then it gets to be 10 istead and the ean number becomes 14 digits instead of 13. How can I solve that in the function?

Thanks for help!

Peter
Expert of the Quarter 2010
Expert of the Year 2010

Commented:
       ChecksumEAN13 = (10 - (total Mod 10)) Mod 10

Regards

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial