Check digits in bar codes are used to check for an error in scanning. It is possible, though unlikely, for a scanner to mis-decode a bar code symbol. When this happens, the wrong data is transmitted to the host system. Check digits help prevent this from happening by using a pre-defined mathematical formula to calculate a "check-digit" for the data represented by the barcode. The most common symbologies to use check digits are UPC and its international counterpart EAN, and Code 128 (typically found on shipping labels).
This check digit typically becomes the last digit represented in the barcode. When the barcode is scanned, the decoder calculates the expected check character based on the values it just decoded and checks its answer against the value of the last character it scanned. If these numbers match, the decoder has validated that the string of numbers scanned is correct and transmits the bar code data to the host system. If the check digits do not match, a no-read occurs and no data is transmitted to the host system.
Even when the decoder finds a valid match with a check digit, there is still the possibility that the transmitted data could be wrong. This is because the most common check digit algorithm, used for UPC / EAN bar codes, must be based on a formula using only numeric digits. Consequently, if there are two transpositions during the decode of the symbol, the algorithm may validate the check digit. The final check that is applied is the match with data in the file of the host system.
The following algorithm is used to calculate the check digit for UPC:
Digit positions are numbered from right to left in this algorithm so that the check digit position counts as position 1 and the number system character counts as position 12. For example, the U.P.C. number 012345-67890 would break out as:
1. Starting from position 2 on the right, add each alternate digit from right to left.
2. Multiply the result of step 1 by 3
3. Starting from position 3 on the right, add the odd position digits.
4. Add result of step 2 to the result of step 3.
5. The check digit is the smallest number which when added to the result of step 4. produces an exact multiple of 10.
For example, the check digit for the number 01234567890 is calculated as follows:
0+8+6+4+2+0 = 20
20 x 3 = 60
9+7+5+3+1 = 25
60 + 25 = 85
85 + 5 = 90 - therefore the check digit is 5.
Each character has a value ranging from 0 to 105. This value is used to calculate the check character for each symbol.
The check character is a Modulus 103 Checksum that is calculated by summing the start code value plus the product of each character position (most significant character position equals 1) and the character value of the character at that position. This sum is divided by 103. The remainder of the answer is the value of the Check Character. Every encoded character is included except the Stop and Check Character. The check character for a Code 128 symbol is never printed in human readable form because it may be an unprintable character.
Example "barcode 1" Message : Start B B a r C o d e 1 Value 104 34 65 82 35 79 68 69 0 17 Position: - 1 2 3 4 5 6 7 8 9 Calculate Total: 104 + (34x1) + (65x2) + (82x3) + (35x4) + (79x5) + (68x6) + (69x7) + (0x8) + (17x9) = 2093 2093/103 = 20 remainder 33 33 = A Final message: (Start B)BarCode 1(A)(STOP)