Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#
Article

Verhoeff Check Digit in C#

Rate me:
Please Sign up or sign in to vote.
4.63/5 (10 votes)
18 Oct 2006CPOL4 min read 84.2K   2.9K   45   9
Calculate and verify check digits using the Verhoeff scheme.

Sample Image - verhoeff_check_digit.jpg

Introduction

A check digit is a decimal (or alphanumeric) digit added to a number for the purpose of detecting the sorts of errors that humans typically make on data entry. Check digits are used for many purposes, such as credit card numbers and International Standard Book Numbers (ISBN). There are a number of schemes and algorithms used for calculating check digits. Some useful references are:

This article presents a C# class that can be used to calculate and verify check digits using the Verhoeff scheme.

Background

On a recent project for an appointment booking system I needed to allocate a "booking identification number" to each appointment. When a client arrived for their appointment, the client would enter their booking identification number at a kiosk using a touch screen. It was important to protect against common data entry errors. The inclusion of a check digit in the number seemed sensible.

After researching various check digits schemes, the most appropriate was the Verhoeff scheme. The benefits of this scheme include:

  • It catches more errors than most other schemes (schemes that add more than one check digit can catch more errors, at the expense of adding more digits)
  • It only adds decimal digits (0-9), so does not require an alpha "keyboard" on the touch screen display
  • It can be used on a number with any number of digits

I found an implementation of the Verhoeff scheme in Java code here. This was easily converted to C# and the code provided with this article was based on the Java code.

Using the code

The source code for this arcticle includes two projects.

  • The first project is a class library that provides the implementation of the Verhoeff scheme. This project provides an assembly that can be reused with any application that needs to calculate or validate check digits.
  • The second project is a WinForms application that can be used to enter a number and calculate its check digit, or check whether a number includes a valid check digit. This applications demonstrates the use of the public interface provided by the class library project.

To use the code in your own project you can either use the class library project as a separate assembly for your application and reference it from your project. Alternatively you can copy just the VerhoeffCheckDigit.cs code file into your own project.

Depending on how you want to calculate, store and verify the check digit of a number, use the appropriate methods provided by the VerhoeffCheckDigit class. This will depend on the data type you want to use for your numbers ande whether you want to include the check digit as part of the number to which it applies (appended at the end) or keep it separate.

VerhoeffCheckDigit Class

In the class library project, the VerhoeffCheckDigit class implements the algorithm. Static methods are provided to calculate and verify check digits.

AppendCheckDigit Method

The AppendCheckDigit method calculates the Verhoeff check digit for a given number provided as input, then returns the input with the check digit appended at the end. Four overloads of this method allow for different data types as input and return value.

  • string - This allows a check digit to be calculated for an arbitrary long number
  • long
  • int
  • int[] - The check digit is added as the last element in the array

CalculateCheckDigit Method

The CalculateCheckDigit method calculates and returns the Verhoeff check digit for a given number provided as input. Four overloads of this method provide for different data types as input. The return value is an int for each overload.

  • string
  • long
  • int
  • int[]

Check Method

The Check method verifies the check digit for a number. Eight overloads of this method provide for different data types as input, and whether the check digit is included as part of the input number or as a separate number. The return value is a bool for each overload: (true if the check digit is valid, false otherwise). Use one of the first four overloads if the check digit is the last digit in the input. Use one of the last four overloads if the check digit is separate to the input.

  • string
  • long
  • int
  • int[]
  • string, int
  • long, int
  • int, int
  • int[], int

Summary

The Verhoeff scheme for check digits is a great choice for many common scenarios where you want to detect the sorts of errors that humans typically make on data entry. If you need to implement this in your application I hope this article is of some help.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionVerhoeff Check Digit Copyright Pin
Gerardo Vallejo21-May-13 4:19
Gerardo Vallejo21-May-13 4:19 
AnswerRe: Verhoeff Check Digit Copyright Pin
David Hay22-May-13 15:14
David Hay22-May-13 15:14 
GeneralMy vote of 5 Pin
Woody10018-Dec-12 15:24
Woody10018-Dec-12 15:24 
GeneralSmal bugs Pin
alexey.nayda18-Oct-06 5:49
alexey.nayda18-Oct-06 5:49 
Take a look on line 301 in VerhoeffCheckDigit.cs
If length of the input array more then 10 items then the power will get negative sign.
The "power" variable should be long or you can use Math.Pow function and then convert the result value to long.

Regards,
Alexey
GeneralRe: Smal bugs Pin
David J Hay18-Oct-06 14:01
David J Hay18-Oct-06 14:01 
GeneralRe: Smal bugs Pin
alexey.nayda19-Oct-06 7:46
alexey.nayda19-Oct-06 7:46 
GeneralNice Pin
NinjaCross11-Oct-06 22:27
NinjaCross11-Oct-06 22:27 
GeneralRe: Nice Pin
David J Hay12-Oct-06 2:42
David J Hay12-Oct-06 2:42 
GeneralRe: Nice Pin
Jeffrey Walton8-Dec-06 12:19
Jeffrey Walton8-Dec-06 12:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.