How to check an EAN13 barcode’s CRC

An EAN13 barcode has a built-in checksum to help cash registers discover damaged barcodes. You can easily check these checksums by hand.

StandardDigit positions
EAN-8N1N2N3N4N5N6N7N8
EAN-12N1N2N3N4N5N6N7N8N9N10N11N12
EAN-13N1N2N3N4N5N6N7N8N9N10N11N12N13
EAN-14N1N2N3N4N5N6N7N8N9N10N11N12N13N14
SSCCN1N2N3N4N5N6N7N8N9N10N11N12N13N14N15N16N17N18
Step 1: Multiply value of each position by
x3x1x3x1x3x1x3x1x3x1x3x1x3x1x3x1x3
Step 2: Add results together to create sum
Step 3: Subtract the sum from nearest equal or higher multiple of ten = Check Digit

The following table gives an example to illustrate how a Check Digit is calculated:

PosN1N2N3N4N5N6N7N8N9N10N11N12N13
Number without Check Digit123456789123-
Step 1: Multiplyxxxxxxxxxxxx-
by131313131313-
Step 2: Add results============-
to create sum163125187249329= 91
Step 3: Subtract the sum from nearest equal or higher multiple of ten = 100-99 = 1
Number with Check Digit1234567891231

My PHP code to check the CRC code:

function ean13_check_digit($digits){
   $digits=preg_split("//",$digits,-1,PREG_SPLIT_NO_EMPTY);
   $a=$b=0;
   for($i=0;$i<6;$i++){
      $a+=(int)array_shift($digits);
      $b+=(int)array_shift($digits);
   }
   $total=($a*1)+($b*3);
   $nextten=ceil($total/10)*10;
   return $nextten-$total==array_shift($digits);
}
 
 
echo ean13_check_digit('1234567891231')?"good":"wrong";
echo ean13_check_digit('1234567891232')?"good":"wrong";
echo ean13_check_digit('1234567891233')?"good":"wrong";
echo ean13_check_digit('1234567891234')?"good":"wrong";
echo ean13_check_digit('1234567891235')?"good":"wrong";
echo ean13_check_digit('1234567891236')?"good":"wrong";
echo ean13_check_digit('1234567891237')?"good":"wrong";
© GeekLabInfo How to check an EAN13 barcode's CRC is a post from GeekLab.info. You are free to copy materials from GeekLab.info, but you are required to link back to http://www.geeklab.info

1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 1.67 out of 5)
Loading...