Check Digit

A check digit is an extra calculated digit added to the end of a code number. The extra digit is calculated from the other digits of the code number.

The Global Trade Item Number (GTIN) used in a barcode contains a check digit. After a barcode is scanned, the pattern of bars is converted to the sequence of digits it represents. A calculation on this sequence must confirm that it is valid before the acceptance of the GTIN is indicated with a beep. A small product such as chewing gum is likely to have a barcode representing eight digits (GTIN-8).

To calculate the check digit of a GTIN-8 code:
  1. Multiply each digit by its weight given in the table below.
  2. Add the products to obtain the sum.
  3. Subtract the sum from nearest equal or higher multiple of ten to obtain the check digit.
Digit position 1 2 3 4 5 6 7 8
Weight (multiplier) 3 1 3 1 3 1 3  

For example, find the check digit of a GTIN-8 barcode when the first 7 digits are 5018438.

Sum = (5 x 3) + (0 x 1) + (1 x 3) + (8 x 1) + (4 x 3) + (3 x 1) + (8 x 3) 
    = 15 + 3 + 8 + 12 + 3 + 24
    = 65
Nearest equal or higher multiple of ten = 70
70 – 65 = 5 (check digit)

The following pseudocode is equivalent to step 3 and will be used in the demonstration program GTIN8:

LastDigitOfSum = Sum MOD 10
IF LastDigitOfSum = 0 THEN
    CheckDigit = 0
ELSE
    CheckDigit = 10 - LastDigitOfSum
program GTIN8;
{$APPTYPE CONSOLE}
uses
  SysUtils;

function CalcCheckDigit(str7CharCode : string) : integer;
var
  intSum, intDigit, intCharPos, intProduct, intLastDigitOfSum : integer;
  charDigit : char;
begin
  intSum := 0;
  for intCharPos := 1 to 7 do
    begin
      charDigit := str7CharCode[intCharPos];
      intDigit :=  StrToInt(charDigit);
      if odd(intCharPos) then
        begin
          intProduct := intDigit * 3;
        end
      else
        begin
          intProduct := intDigit;
        end;
          intSum := intSum + intProduct;
    end;
  intLastDigitOfSum := intSum mod 10;
  if intLastDigitOfSum = 0 then
    begin
      result := 0;
    end
  else
    begin
      result := 10 - intLastDigitOfSum;
    end;
end;

var
  intDummy, intErrorPos, intCheckDigit : integer;
  strInput, strGTIN8 : string;
  charCheckDigit : char;

begin
  //Ensure that 7 digits are input.
  repeat
    write('Please enter the first 7 digits of the GTIN-8 code. ');
    readln(strInput);
    val(strInput, intDummy, intErrorPos);
  until (length(strInput) = 7) and (intErrorPos = 0);
  //Calculate the check digit for the validated input string
  intCheckDigit := CalcCheckDigit(strInput);
  //Convert from integer to char.  The ASCII code for '0' is 48.
  charCheckDigit := chr(intCheckDigit + 48);
  //Generate the 8 character code.
  strGTIN8 := strInput + charCheckDigit;

  writeln('Check digit: ',intCheckDigit);
  writeln('Eight digit code: ',strGTIN8);
  readln;
end.

You may prefer to use a function that processes the input data as an integer rather than a string. A second version of the program demonstrates this approach.

program GTIN8v2;
{$APPTYPE CONSOLE}
uses
  SysUtils;

function CalcCheckDigit(int7CharCode : integer) : integer;
var
  intSum, intDigit, intCodeDiv, intDigitPos, intProduct, intLastDigitOfSum : integer;
  charDigit : char;
begin
  intSum := 0;
  intCodeDiv := int7CharCode;
  {Successive use of the mod and div operators will
  give the 7 digits of the code in reverse order.}
  for intDigitPos := 7 downto 2 do
    begin
      intDigit :=  intCodeDiv mod 10;
      intCodeDiv := intCodeDiv div 10;
      if odd(intDigitPos) then
        begin
          intProduct := intDigit * 3;
        end
      else
        begin
          intProduct := intDigit;
        end;
          intSum := intSum + intProduct;
    end;
    //intCodeDiv contains the first digit
    intSum := intSum + intCodeDiv * 3;
  intLastDigitOfSum := intSum mod 10;
  if intLastDigitOfSum = 0 then
    begin
      result := 0;
    end
  else
    begin
      result := 10 - intLastDigitOfSum;
    end;
end;

var
  int7DigitCode, intErrorPos, intCheckDigit : integer;
  strInput, strGTIN8 : string;
  charCheckDigit : char;

begin
  //Ensure that 7 digits are input.
  repeat
    write('Please enter the first 7 digits of the GTIN-8 code. ');
    readln(strInput);
    val(strInput, int7DigitCode, intErrorPos);
  until (length(strInput) = 7) and (intErrorPos = 0);
  //Calculate the check digit for the validated integer
  intCheckDigit := CalcCheckDigit(int7DigitCode);
  //Convert from integer to char.  The ASCII code for '0' is 48.
  charCheckDigit := chr(intCheckDigit + 48);
  //Generate the 8 character code.
  strGTIN8 := strInput + charCheckDigit;

  writeln('Check digit: ',intCheckDigit);
  writeln('Eight digit code: ',strGTIN8);
  readln;
end.  
      
     
Programming - a skill for life!

Checking data to see if it is sensible