Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C++

Error Detection Based on Check Digit Schemes

Rate me:
Please Sign up or sign in to vote.
4.72/5 (27 votes)
27 Nov 2007CPOL12 min read 90.8K   2.9K   50  
A Survey of Popular Check Digit Schemes
/*
 * Copyright 2005, Nick Galbreath
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software for any purpose
 * with or without fee is hereby granted, provided that the above copyright
 * notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
 * OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 * Except as contained in this notice, the name of a copyright holder shall not
 * be used in advertising or otherwise to promote the sale, use or other dealings
 * in this Software without prior written authorization of the copyright holder. 
 */
package com.modp.checkdigits;

/**
 * Implements ISO 7064 Mod 11,2 check digit scheme.
 * 
 * @author nickg
 * @version 1
 */
public class CheckISO7064Mod11_2 implements CheckDigit {

	/* (non-Javadoc)
	 * @see com.modp.checkdigit.CheckDigit#encode(java.lang.String)
	 */
	public String encode(String digits) {
		int c = computeCheck(digits);
		if (c == 10) {
			return digits + 'X';
		} else {
			return digits + c;
		}
	}

	/* (non-Javadoc)
	 * @see com.modp.checkdigit.CheckDigit#verify(java.lang.String)
	 */
	public boolean verify(String digits) {
		// normally I'd redo the algorith, but with check digit
		// using 0-9 + X, it's easiler this way.
		try {
			return computeCheck(getData(digits)) == getCheckDigit(digits);
		} catch (Exception e) {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see com.modp.checkdigit.CheckDigit#computeCheck(java.lang.String)
	 */
	public int computeCheck(String digits) {
		int p = 0;
		for (int i = 0; i < digits.length(); ++i) {
			int c = digits.charAt(i) - '0';
		     if (c < 0 || c > 9) {
		     	throw new NumberFormatException("'" + digits + "' has bad digit: '" + digits.charAt(i) + "'");
			}
		    p = 2*(p + c);
		}
		p = p % 11;
		// check + p == 1 Mod 11
		return (12 - p) % 11;
		// could also do
		//if (p <= 1) { return 1 -p} else { return 12 -p}
	}

	/* (non-Javadoc)
	 * @see com.modp.checkdigit.CheckDigit#getCheckDigit(java.lang.String)
	 */
	public int getCheckDigit(String digits) {
		char c = digits.charAt(digits.length() -1);
		if (c == 'X' || c == 'x') {
			return 10;
		} else {
			return c - '0';
		}
	}

	/* (non-Javadoc)
	 * @see com.modp.checkdigit.CheckDigit#getData(java.lang.String)
	 */
	public String getData(String digits) {
		return digits.substring(0, digits.length() -1);
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Systems / Hardware Administrator
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions