Skip to content

Instantly share code, notes, and snippets.

@kirill578
Created October 5, 2021 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirill578/0a9d0abfb4e36dadc96006df59db390b to your computer and use it in GitHub Desktop.
Save kirill578/0a9d0abfb4e36dadc96006df59db390b to your computer and use it in GitHub Desktop.
double luhn checksum barcode
' Output:
'100000000 -> A10000000086
'100000001 -> A10000000164
'100000002 -> A10000000242
'100000003 -> A10000000320
'100000004 -> A10000000407
'100000005 -> A10000000575
Module VBModule
Sub Main()
Dim i As Integer
For i = 0 To 5 Step 1
Dim val = 100000000 + i
Dim newVal = barcode(val)
Console.WriteLine(Str(val) + " -> " + newVal)
Next i
End Sub
Function barcode(value As Integer) As String
barcode = ("A" & Str(value) & Str(luhnSum(value)) & Str(luhnSum(value + 1))).replace(" ", "")
End Function
Function luhnSum(InVal As String) As Integer
Dim sum As Integer
sum = 0
Dim strLen As Integer
strLen = Len(InVal)
Dim i As Integer
For i = strLen To 1 Step -1
Dim digit As Integer
digit = CInt(Mid(InVal, i, 1))
If ((i Mod 2) = 1) Then
digit = digit * 2
If (digit > 9) Then
digit = digit - 9
End If
End If
sum = sum + digit
Next i
sum = sum * 9
luhnSum = sum Mod 10
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment