Link to home
Start Free TrialLog in
Avatar of Peter Nordberg
Peter NordbergFlag for Sweden

asked on

Calculate checksum for Ean13 barcode number

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
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Avatar of Peter Nordberg

ASKER

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

Peter
   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
Hi cyberkiwi,

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

Peter
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
VS 2008
And you are using VB.Net right?
That is weird - I built and tested it in vb.net/vs2008!
Yes, vb.net. VS 2008
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
ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!

That worked perfectly!

Peter
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
       ChecksumEAN13 = (10 - (total Mod 10)) Mod 10

Regards