Click here to Skip to main content
15,880,967 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.7K   2.9K   50  
A Survey of Popular Check Digit Schemes
#include "StdAfx.h"

#include <assert.h>

int CharToNumber( TCHAR c )
{    
    switch ( c )
    {
    case '0': return  0;
    case '1': return  1;
    case '2': return  2;
    case '3': return  3;
    case '4': return  4;
    case '5': return  5;
    case '6': return  6;
    case '7': return  7;
    case '8': return  8;
    case '9': return  9;
    case 'a': case 'A': return 10;
    case 'b': case 'B': return 11;
    case 'c': case 'C': return 12;
    case 'd': case 'D': return 13;
    case 'e': case 'E': return 14;
    case 'f': case 'F': return 15;
    case 'g': case 'G': return 16;
    case 'h': case 'H': return 17;
    case 'i': case 'I': return 18;
    case 'j': case 'J': return 19;
    case 'k': case 'K': return 20;
    case 'l': case 'L': return 21;
    case 'm': case 'M': return 22;
    case 'n': case 'N': return 23;
    case 'o': case 'O': return 24;
    case 'p': case 'P': return 25;
    case 'q': case 'Q': return 26;
    case 'r': case 'R': return 27;
    case 's': case 'S': return 28;
    case 't': case 'T': return 29;
    case 'u': case 'U': return 30;
    case 'v': case 'V': return 31;
    case 'w': case 'W': return 32;
    case 'x': case 'X': return 33;
    case 'y': case 'Y': return 34;
    case 'z': case 'Z': return 35;    

    default: assert( false );
    }
    return -1;
}

TCHAR NumberToChar( int d )
{
    if( d >=  0  && d <=  9 ) { return d + '0'; }

    if( d >= 10  && d <= 35 ) { return d - 10 + 'A'; }

    return 0x00;
}

int StringToNumber( _tstring s )
{    
	_tistringstream ss( s );

    int n;
    
	ss >> n;

    return n;
}

_tstring NumberToString( int n )
{    
	_tostringstream oss( _tostringstream::out );
    
    oss << n << std::ends;

    return oss.str();
}

/*
_tstring NumberToString( double d )
{    
	_tostringstream oss( _tostringstream::out );
    
    oss.setf(std::ios::fixed);
    oss.precision(4);

    oss << d << std::ends;

    return oss.str();
}
*/

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