[zenithpaints]

Checksum Validation
Last Update: 20 July, 2002 10:20 AM


    With the 4240 project you will need to validate two checksums, one being 5 digits and the other 6 digits. The checksum calculation is quite simple and explained within the spec, so I will not repeat it here. You may write two functions to do the validating, but it is possible to write just the one. I will show you how to do the checksum validation using a simple single function.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*-- 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)
  { 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)
{ size_t 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;
}

    I will not go through this code one line at a time, but instead just explain how it works. The main function is only there so that you may enter a number and have the program display it's checksum. To use this code in your program you will need to change this functionality by returning either true or false to the calling function depending on whether the checksum is correct like this :

#define CKSUM_WEIGHT_6 "65432"
#define CKSUM_WEIGHT_5 "5432"

int checksum (char *value)
{ int    temp = 0,
         x;
  size_t count;

  /*-- Minus zero (- '0') converts the char value to an int ('9' to 9) --*/
  count=strlen(value);
  for (x = 0; x < (count-1); x++)
  { if (count == 5)
      temp+=((*value++ - '0') * (CKSUM_WEIGHT_5[x] - '0'));
    else if (count ==6)
      temp+=((*value++ - '0') * (CKSUM_WEIGHT_6[x] - '0'));
    else
      return ERROR;
  }
  temp = (11 - (temp % 11));

  if (((temp == 11) && (*value == 'X'))
    || ((temp == 10) && (*value == '0'))
    || ((temp == (*value - '0')))
    return SUCCESS;
  else
    return ERROR;
}

    This code firstly stores the length of the string passed to it. It then loops through the given string calculating the checksum. If the string length is not equal to 5 or 6, then an incorrect string has been passed and ERROR is returned. ERROR is also returned if the checksum appended to the given string is incorrect. If the checksum appended to the given string is correct then SUCCESS is returned. The checksum that we calculate is stored in the variable temp, the given string checksum is checked against this value to determine if the given strings checksum was correct or not. This code may be a little confusing to start with but if you mess around with the command line version and get a feel for what it is doing then start to mess with the function itself. When you fully understand the function then feel free to improve it and add it to your own program.

Back Home

© Copyright 1999-2008 @incubus. All Rights Reserved. All trademarks acknowledged.