13,351,954 members (87,723 online)
alternative version

#### Stats

66.8K views
45 bookmarked
Posted 11 Oct 2006

# Verhoeff Check Digit in C#

, 18 Oct 2006
 Rate this:
Calculate and verify check digits using the Verhoeff scheme.

## 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.

## Share

 Web Developer Australia
No Biography provided

## You may also be interested in...

 First Prev Next
 Verhoeff Check Digit Copyright Gerardo Vallejo21-May-13 5:19 Gerardo Vallejo 21-May-13 5:19
 Re: Verhoeff Check Digit Copyright David Hay22-May-13 16:14 David Hay 22-May-13 16:14
 My vote of 5 Woody10018-Dec-12 16:24 Woody100 18-Dec-12 16:24
 Smal bugs anayda18-Oct-06 6:49 anayda 18-Oct-06 6:49
 Re: Smal bugs David J Hay18-Oct-06 15:01 David J Hay 18-Oct-06 15:01
 Re: Smal bugs anayda19-Oct-06 8:46 anayda 19-Oct-06 8:46
 Yep! I've saw also the second bug A bit later, when i'll have a free minute, i'll try to look for improvment of current implementation performance. Kind regards, Alexey
 Nice NinjaCross11-Oct-06 23:27 NinjaCross 11-Oct-06 23:27
 Re: Nice David J Hay12-Oct-06 3:42 David J Hay 12-Oct-06 3:42
 Re: Nice Jeffrey Walton8-Dec-06 13:19 Jeffrey Walton 8-Dec-06 13:19
 Last Visit: 31-Dec-99 19:00     Last Update: 20-Jan-18 9:09 Refresh 1