/*************************************************************\ * inc - 22-08-2001 - checksum.c -- http://www.incubus.co.uk * * Free code, so don't come crying to me if it fouls up || * * Windows(DOS) and Linux compatable code * * Calculates a modulus 11 checksum, 6 or 5 digit weights * * * * This code is not perfect but it does the job. I would be * * grateful if you sent me improvements made to this code. * \*************************************************************/ #include #include #include /*-- Check Sum Weights --*/ #define MOD_WEIGHT_6 "65432" #define MOD_WEIGHT_5 "5432" #define SUCCESS 1 #define ERROR 0 int checkSum (char *value); int main (int argc, char *argv[]) { if (argc == 2) { if(!checkSum(argv[1])) printf("Number incorrect length (4-5 digits only)\n"); } else { printf("Please enter a number with the checkdigit missing -\n"); printf(" 1234 = 5th digit will be the checkdigit\n"); printf(" 12345 = 6th digit will be the checkdigit\n"); } return EXIT_SUCCESS; } int checkSum (char *value) { int count; int temp=0; int x; count=strlen(value); for (x=0; x < count; x++) if (count == 4) temp+=((value[x] -48) * (MOD_WEIGHT_5[x]-48)); else if (count == 5) temp+=((value[x] -48) * (MOD_WEIGHT_6[x]-48)); else return ERROR; count = (11-(temp%11)); printf("Number with checkdigit appended = %s",value); if (count == 11) printf("X\n"); else if (count == 10) printf("0\n"); else printf("%d\n",count); return SUCCESS; }