Peter Nordberg
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
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
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=43547&lngWId=1
ASKER
Hi and thanks for your code, but it doesn't seem to calculate the right checksum number.
Peter
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
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
ASKER
Hi cyberkiwi,
I can't get barcode.Reverse() to work. Do I need to import some namespace or?
Peter
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 .ToCharArr ay()))
For Each el As Char In barcode
... rest
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
For Each el As Char In barcode
... rest
ASKER
VS 2008
And you are using VB.Net right?
That is weird - I built and tested it in vb.net/vs2008!
That is weird - I built and tested it in vb.net/vs2008!
ASKER
Yes, vb.net. VS 2008
ASKER
When I use your latest code I get the error message on this row:
barcode = CStr(Array.Reverse(barcode .ToCharArr ay()))
It says that the expression doesn't produce a value.
Peter
barcode = CStr(Array.Reverse(barcode
It says that the expression doesn't produce a value.
Peter
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
That worked perfectly!
Peter
That worked perfectly!
Peter
ASKER
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
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
Regards