Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Luhn's Algorithm in Visual Basic

From Wiki

Jump to: navigation, search

Well, if you are stumbling upon these FAQ's you probably know what luhn's (mod 10) algorithm is, but basically it is used to assign check digits and validate things like credit card account numbers.

See here for more info: Luhn Algorithm - Wikipedia

I recently had to implement this in VB6/A, and I saw examples out there for everything except VB6 so I thought this would be helpful to someone.

There are two functions (LuhnCheckDigit gives you the check digit, LuhnValid checks numbers with an included check digit for validity). The function LookupArray is used to return an array used in looking up some of the values (I chose to do it like this to make the coding a little cleaner).

Onto the code (copious comments included)-->

Declaration Section & Function to create lookup array

  1. Option Explicit
  2.  
  3. 'Luhn algorithm overview (from wikipedia.com)
  4. 'The formula verifies a number against its included check digit
  5. ', which is usually appended to a partial account number to
  6. 'generate the full account number.
  7. 'This account number must pass the following test:
  8.  
  9. '1. Starting with the rightmost digit (which is the check digit) and moving left,
  10. '   double the value of every second digit. For any digits that thus become 10 or
  11. '   more, add their digits together as if casting out nines. For example, 1111
  12. '   becomes 2121, while 8763 becomes 7733 (from 2×6=12 ? 1+2=3 and 2×8=16 ? 1+6=7)
  13.  
  14. '2. Add all these digits together. For example, if 1111 becomes 2121, then 2+1+2+1
  15. '   is 6; and 8763 becomes 7733, so 7+7+3+3 is 20.
  16. '3. If the total ends in 0 (put another way, if the total modulus 10 is congruent to 0)
  17. '   , then the number is valid according to the Luhn formula; else it is not valid. So,
  18. '   1111 is not valid (as shown above, it comes out to 6), while 8763 is valid (as
  19. '   shown above, it comes out to 20).
  20.  
  21.  
  22. Private Function LookupArray() As Integer()
  23.  
  24. 'this function is used to provide an array used to lookup for 'doubled' digits
  25.  
  26. Dim aXL(9) As Integer
  27.  
  28. 'array values in xL are for the index integer
  29. '0 * 2 = 0 --> 0 = 0
  30. '6*2 = 12 --> 1 + 2 = 3
  31. 'having this array available saves us from performing string conversions
  32. 'and math operations (just lookup by index)
  33. aXL(0) = 0
  34. aXL(1) = 2
  35. aXL(2) = 4
  36. aXL(3) = 6
  37. aXL(4) = 8
  38. aXL(5) = 1
  39. aXL(6) = 3
  40. aXL(7) = 5
  41. aXL(8) = 7
  42. aXL(9) = 9
  43.  
  44. LookupArray = aXL
  45.  
  46. End Function

Check Digit Assignment:

  1. Public Function LuhnCheckDigit(ByVal intStr As String) As String
  2.  
  3. 'this function is used to return the check digit to be appended to a given number
  4. Dim b() As Byte
  5. Dim x As Integer
  6. Dim xL() As Integer
  7. Dim sD As Integer ' this holds sum of digits (as modified by Luhn algorithm)
  8. Dim lD As Integer ' this is used to store checksum digit (10 - sD Mod 10)
  9.  
  10. 'check for numeric input
  11. If Not IsNumeric(intStr & ".0e0") Then
  12.    LuhnCheckDigit = "X"
  13.    Exit Function
  14. End If
  15.  
  16. 'create lookup array for loop
  17. xL = LookupArray()
  18.  
  19. sD = 0
  20. ReDim b(Len(intStr))
  21.  
  22. b = StrConv(StrReverse(intStr), vbFromUnicode)
  23.  
  24. 'b(x) - 48 == faster way to get integer value from unicode byte value
  25. 'first digit (starting from right)is doubled/digits added because once
  26. 'check digit is appended this will be the second
  27. For x = LBound(b) To UBound(b)
  28.     If x Mod 2 = 0 Then
  29.         sD = sD + xL(b(x) - 48)
  30.     Else
  31.         sD = sD + (b(x) - 48)
  32.     End If
  33. Next
  34.  
  35. lD = 10 - (sD Mod 10)
  36.  
  37. 'we don't want to add 10, if lD calculates to 10 then we really want to add 0
  38. If lD = 10 Then
  39.     lD = 0
  40. End If
  41.  
  42. 'return string with check digit appended
  43. LuhnCheckDigit = CStr(lD)
  44.  
  45. End Function

Validation Function:

  1. Public Function LuhnValid(ByVal intStr As String) As Boolean
  2.  
  3. 'this function is used to check if a number entered is valid
  4. Dim sD As Integer
  5. Dim bl As Boolean
  6. Dim b() As Byte
  7. Dim x As Integer
  8. Dim xL() As Integer
  9.  
  10. 'check for numeric input
  11. If Not IsNumeric(intStr & ".0e0") Then
  12.    LuhnValid = False
  13.    Exit Function
  14. End If
  15.  
  16. 'create lookup array for loop
  17. xL = LookupArray()
  18.  
  19. ReDim b(Len(intStr))
  20.  
  21. b = StrConv(intStr, vbFromUnicode)
  22.  
  23. bl = False
  24. sD = 0
  25.  
  26. 'start with last digit, work towards first
  27. For x = UBound(b) To LBound(b) Step -1
  28.     If bl Then
  29.         sD = sD + xL(b(x) - 48)
  30.     Else
  31.         sD = sD + b(x) - 48
  32.     End If
  33.    
  34.     bl = Not (bl)
  35. Next
  36.  
  37. LuhnValid = (sD Mod 10 = 0)
  38.  
  39. End Function

It is fairly simple to use, here are some example:

  1. 'to add a check digit to a number:
  2. NumberWithCheck = CStr(Number) & LuhnCheckDigit(Number)
  3.  
  4. 'verify a number with check digit included:
  5. Valid = LuhnValid(NumberWithCheck)

I hope that this will help someone out in the future.

Thanks to ESquared and gmmastros for their assistance in getting this to work just so.

92 Rating: 2.3/5 (6 votes cast)