An EAN13 barcode has a built-in checksum to help cash registers discover damaged barcodes. You can easily check these checksums by hand.
Standard | Digit positions | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
EAN-8 | N1 | N2 | N3 | N4 | N5 | N6 | N7 | N8 | |||||||||||
EAN-12 | N1 | N2 | N3 | N4 | N5 | N6 | N7 | N8 | N9 | N10 | N11 | N12 | |||||||
EAN-13 | N1 | N2 | N3 | N4 | N5 | N6 | N7 | N8 | N9 | N10 | N11 | N12 | N13 | ||||||
EAN-14 | N1 | N2 | N3 | N4 | N5 | N6 | N7 | N8 | N9 | N10 | N11 | N12 | N13 | N14 | |||||
SSCC | N1 | N2 | N3 | N4 | N5 | N6 | N7 | N8 | N9 | N10 | N11 | N12 | N13 | N14 | N15 | N16 | N17 | N18 | |
Step 1: Multiply value of each position by | |||||||||||||||||||
x3 | x1 | x3 | x1 | x3 | x1 | x3 | x1 | x3 | x1 | x3 | x1 | x3 | x1 | x3 | x1 | x3 | |||
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:
Pos | N1 | N2 | N3 | N4 | N5 | N6 | N7 | N8 | N9 | N10 | N11 | N12 | N13 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Number without Check Digit | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 1 | 2 | 3 | - |
Step 1: Multiply | x | x | x | x | x | x | x | x | x | x | x | x | - |
by | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | - |
Step 2: Add results | = | = | = | = | = | = | = | = | = | = | = | = | - |
to create sum | 1 | 6 | 3 | 12 | 5 | 18 | 7 | 24 | 9 | 3 | 2 | 9 | = 91 |
Step 3: Subtract the sum from nearest equal or higher multiple of ten = 100-99 = 1 | |||||||||||||
Number with Check Digit | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 1 | 2 | 3 | 1 |
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"; |