13,057,050 members (43,027 online)
Add your own
alternative version

#### Stats

64.9K views
2.2K downloads
45 bookmarked
Posted 11 Oct 2006

# Verhoeff Check Digit in C#

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

## License

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

## About the Author

 Web Developer Australia
No Biography provided

## Comments and Discussions

 First Prev Next
 Verhoeff Check Digit Copyright Gerardo Vallejo21-May-13 4:19 Gerardo Vallejo 21-May-13 4:19
 Re: Verhoeff Check Digit Copyright David Hay22-May-13 15:14 David Hay 22-May-13 15:14
 My vote of 5 Woody10018-Dec-12 15:24 Woody100 18-Dec-12 15:24
 Smal bugs anayda18-Oct-06 5:49 anayda 18-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
 Re: Smal bugs David J Hay18-Oct-06 14:01 David J Hay 18-Oct-06 14:01
 Re: Smal bugs anayda19-Oct-06 7:46 anayda 19-Oct-06 7:46
 Nice NinjaCross11-Oct-06 22:27 NinjaCross 11-Oct-06 22:27
 Re: Nice David J Hay12-Oct-06 2:42 David J Hay 12-Oct-06 2:42
 Re: Nice Jeffrey Walton8-Dec-06 12:19 Jeffrey Walton 8-Dec-06 12:19
 Last Visit: 31-Dec-99 18:00     Last Update: 29-Jul-17 1:56 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

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

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