# Calculating EAN-8 / EAN-13 check digits with VB .NET

The following two code snippets show how to calculate an EAN8 / EAN13 check digit with Visual Basic .NET. Both routines also test the passed data for correctness. For both code types the passed data must be numeric, for EAN 8 the length must be seven or eight digits, for EAN 13, twelve or thirteen digits.

Obviously, the following routines could be written much shorter and much more elegantly.

The following VB .NET source code has been put into the Public Domain. Use it to generate barcodes with VB .NET or to validate and verify EAN barcodes that have been scanned and decoded.

## EAN 8 Checksum with VB .NET

```Public Class SomeClassName
Private Shared Function _checksum_ean8(data As [String]) As Integer
' Test string for correct length
If data.Length <> 7 AndAlso data.Length <> 8 Then
Return -1
End If

' Test string for being numeric
For i As Integer = 0 To data.Length - 1
If data(i) < &H30 OrElse data(i) > &H39 Then
Return -1
End If
Next

Dim sum As Integer = 0

For i As Integer = 6 To 0 Step -1
Dim digit As Integer = data(i) - &H30
If (i And 1) = 1 Then
sum += digit
Else
sum += digit * 3
End If
Next
Dim [mod] As Integer = sum Mod 10
Return If([mod] = 0, 0, 10 - [mod])
End Function
End Class
```

## EAN 13 Checksum with VB .NET

```Public Class SomeClassName
Private Shared Function _checksum_ean13(data As [String]) As Integer
' Test string for correct length
If data.Length <> 12 AndAlso data.Length <> 13 Then
Return -1
End If

' Test string for being numeric
For i As Integer = 0 To data.Length - 1
If data(i) < &H30 OrElse data(i) > &H39 Then
Return -1
End If
Next

Dim sum As Integer = 0

For i As Integer = 11 To 0 Step -1
Dim digit As Integer = data(i) - &H30
If (i And 1) = 1 Then
sum += digit
Else
sum += digit * 3
End If
Next
Dim [mod] As Integer = sum Mod 10
Return If([mod] = 0, 0, 10 - [mod])
End Function
End Class
```
If you are interested in creating barcodes with VB .NET, make sure to see our .NET barcode SDK.