Click here to Skip to main content
Click here to Skip to main content

Verhoeff Check Digit in C#

, 18 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

David Hay
Web Developer
Australia Australia
No Biography provided

Comments and Discussions

 
QuestionVerhoeff Check Digit Copyright PinmemberGerardo Vallejo21-May-13 5:19 
AnswerRe: Verhoeff Check Digit Copyright PinmemberDavid Hay22-May-13 16:14 
GeneralMy vote of 5 PinmemberWoody10018-Dec-12 16:24 
GeneralSmal bugs Pinmemberanayda18-Oct-06 6:49 
GeneralRe: Smal bugs PinmemberDavid J Hay18-Oct-06 15:01 
GeneralRe: Smal bugs Pinmemberanayda19-Oct-06 8:46 
GeneralNice PinmemberNinjaCross11-Oct-06 23:27 
GeneralRe: Nice PinmemberDavid J Hay12-Oct-06 3:42 
GeneralRe: Nice PinmemberJeffrey Walton8-Dec-06 13:19 
http://www.codeproject.com/useritems/CheckDigit.asp[^]

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411022.1 | Last Updated 18 Oct 2006
Article Copyright 2006 by David Hay
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid