EAN-13 is the 13-digit retail barcode used on products in most of the world, UPC-A is the 12-digit version used in the United States and Canada, and Code 128 is the flexible choice for shipping labels, stock codes and order numbers. For EAN-13 and UPC-A the last digit is a check digit: you multiply the other digits alternately by 1 and 3, add them up, and the check digit is whatever brings the total to the next multiple of 10. This article shows the calculation with real numbers, explains what it can and cannot catch, and then does the same for Code 128.
Which type for which job
| Type | What it encodes | Typical use |
|---|---|---|
| EAN-13 | 13 digits | Products sold in shops, books (ISBN-13), most of the world |
| UPC-A | 12 digits | Products in the US and Canada |
| EAN-8 | 8 digits | Packages too small for an EAN-13 |
| ITF-14 | 14 digits | Cartons and cases of retail goods |
| Code 128 | Any basic ASCII text, up to 80 characters in the generator | Shipping labels, asset tags, order numbers, internal stock codes |
| Code 39 | Capital letters, digits and a few symbols | Older systems that still require it |
A UPC-A is the same as an EAN-13 with a leading zero. A scanner that reads EAN-13 reads a UPC-A as 0 followed by its 12 digits. A barcode holds only a short code. The price and the product name live in the database of whoever scans it.
The EAN-13 check digit, step by step
Take the first 12 digits of an EAN-13: 5 9 0 1 2 3 4 1 2 3 4 5. The rule is to weight them alternately 1, 3, 1, 3 and so on from the left, add the results, and find the digit that completes the sum to a multiple of 10.
| Digit | 5 | 9 | 0 | 1 | 2 | 3 | 4 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Weight | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 |
| Product | 5 | 27 | 0 | 3 | 2 | 9 | 4 | 3 | 2 | 9 | 4 | 15 |
The products add up to 5 + 27 + 0 + 3 + 2 + 9 + 4 + 3 + 2 + 9 + 4 + 15 = 83. The next multiple of 10 is 90, and 90 minus 83 is 7. The check digit is 7, so the full number is 5901234123457. If the sum were already a multiple of 10, the check digit would be 0.
Another way to remember it: counting from the right, starting with the last digit before the check digit, the weights are 3, 1, 3, 1. That version works for every length of retail number, which is why the same rule covers UPC-A, EAN-8 and ITF-14.
The same calculation for UPC-A
The UPC-A number 03600029145 has 11 digits before the check digit. From the right the first weight is 3, so counting from the left the weights are 3, 1, 3, 1 and so on, ending with 3:
- digits in odd positions (1st, 3rd, 5th, 7th, 9th, 11th): 0, 6, 0, 2, 1, 5, which add up to 14, and 14 × 3 = 42,
- digits in even positions (2nd, 4th, 6th, 8th, 10th): 3, 0, 0, 9, 4, which add up to 16,
- total: 42 + 16 = 58.
The next multiple of 10 is 60, so the check digit is 2, and the full number is 036000291452. Put a 0 in front and you have the EAN-13 0036000291452, whose check digit comes out as 2 as well. The EAN-8 7351353 works the same way: weights 3, 1, 3, 1, 3, 1, 3 give 63, so the check digit is 7 and the number is 73513537.
What a check digit catches
A check digit exists so that a scanner can reject a misread instead of ringing up the wrong product. Two facts follow from the arithmetic:
- Any single wrong digit is caught. The weights are 1 and 3, and neither shares a factor with 10, so a changed digit always shifts the sum off a multiple of 10.
- Most swaps of neighbouring digits are caught, but not all. Swapping two adjacent digits changes the sum by twice their difference. That is a multiple of 10 when the digits differ by exactly 5, so 05 and 50, 16 and 61, 27 and 72, 38 and 83, and 49 and 94 slip through. The check digit reduces errors a great deal. It does not make a number impossible to mistype.
Code 128 checks itself differently
Code 128 does not print a visible check digit, and you never type one. The symbol contains a check character the scanner uses silently. It is calculated like this: start with the value of the start symbol, then add each character's value multiplied by its position (1 for the first character, 2 for the second, and so on), and take the remainder after dividing by 103.
For text such as Hi5 in the Code 128 character set B, the start value is 104 and a character's value is its ASCII code minus 32, so H is 40, i is 73 and 5 is 21:
104 + (1 × 40) + (2 × 73) + (3 × 21) = 104 + 40 + 146 + 63 = 353
353 divided by 103 leaves 44, so the check character has the value 44. A stop symbol follows, and the finished barcode is 68 modules wide, where a module is the narrowest bar.
Digits get special treatment. Set C stores two digits in one symbol, which makes a long numeric code much shorter. For 12345678 the generator switches to set C by itself: start C is 105, and the values are 12, 34, 56 and 78. The sum is 105 + 12 + (2 × 34) + (3 × 56) + (4 × 78) = 105 + 12 + 68 + 168 + 312 = 665, which leaves 47 after dividing by 103. Eight digits take five symbols and the barcode is 79 modules wide, so numeric codes stay compact without any effort from you.
Making one with the barcode generator
The barcode generator does this arithmetic and draws the bars in your browser, so what you type is not uploaded.
- Choose the type, for example EAN-13. The line under the field says what it takes.
- Type the 12 digits, without the check digit. The tool adds it and tells you so, for example "check digit 7 added".
- Or type all 13 digits. The tool verifies the last one. If it is wrong, it shows the digit it should be, and deleting your last digit lets it add the right one.
- Keep dark bars on a light background, leave the empty margin on both sides (the quiet zone), and download the SVG for printing or the PNG for a screen.
- Scan the printed result with the scanner that will be used before you print a thousand labels.
Mistakes to avoid
- Making up product numbers for the shop. A number you invent can clash with someone else's. Retail numbers should come from GS1, the organisation that issues company prefixes, whose rules shops and marketplaces rely on. For internal use, any number is fine, and Code 128 is simpler.
- Typing the number in a spreadsheet first. A leading zero disappears, and the barcode then has the wrong length. Keep the digits as text.
- Counting the weights from the wrong end. For the 12 digits of an EAN-13 the first weight is 1, but for the 11 digits of a UPC-A it is 3. When in doubt, count from the right: the digit next to the check digit always has weight 3.
- Stretching a barcode on one axis. It changes the bar widths and the scan fails.
If what you need is a link or Wi-Fi details instead of a product number, a QR code is the better format.