A barcode that looks perfect on screen and fails at the scanner is almost always a check digit problem. The symbol is drawn correctly, the printer did its job, and the scanner still refuses it, because Code 128 carries a checksum that is calculated from the data and any mismatch makes the whole symbol invalid.

Here is how that checksum works, how to compute it, and the mistakes that produce a barcode which only looks right.

What Code 128 actually encodes

Code 128 is a variable-length symbology with three code sets (A, B and C) and a mandatory check character. Every symbol is 11 modules wide, and each character maps to a value:

  • Code Set B covers the printable ASCII range.
  • Code Set C encodes pairs of digits in a single symbol, which is why numeric IDs are half as long in C.
  • Code Set A covers control characters as well.

The symbol is: start character, the data, the check character, the stop pattern. The stop pattern is 13 modules and is the only one that looks different, which is how a scanner knows where the symbol ends.

The check digit is mod 103

The calculation is short and easy to get subtly wrong:

  1. Take the value of the start character (Start A = 103, Start B = 104, Start C = 105).
  2. Multiply each data character's value by its position, where the first data character is position 1.
  3. Add everything to the start value.
  4. Take the total modulo 103.

That result is the check character's value, and the character with that value is appended before the stop pattern.

CODE128B = {chr(c): c - 32 for c in range(32, 127)} def code128b_checksum(data: str) -> int: start = 104 # Start B total = start for i, ch in enumerate(data, start=1): total += CODE128B[ch] * i return total % 103 # "MB-2026" -> checksum value print(code128b_checksum("MB-2026")) 

If you are using Code Set C, the values are the two-digit numbers themselves (00 to 99), not their ASCII codes, and the position weighting is per symbol rather than per digit. That difference is the first place a hand-written encoder breaks.

The four mistakes that make a valid-looking barcode fail

Using the ASCII value instead of the code set value. In Code Set B, the value of "A" is 33, not 65. Using ASCII gives a checksum that is wrong by a consistent offset, which is why some labels scan and others do not.

Leaving the start character out of the sum. The start value is part of the total. Skip it and every symbol is wrong by 104 (or 103, or 105).

Off-by-one on the position weight. The first data character has weight 1, not 0. Starting the enumerate at zero shifts every weight by one and produces a symbol that fails on almost every scanner.

No quiet zone. Ten modules of clear space on each side of the symbol are part of the specification. A barcode printed hard against a label edge, or next to other text, will fail even with a correct checksum. This is the one that gets blamed on the printer.

Module width decides whether it scans at all

The X-dimension (the width of one module) has a minimum for the printing method and the scan distance. Print too small on a thermal printer and the bars bleed together; print too large and a fixed scanner cannot fit the symbol in view. If a barcode reads on a phone screen but not on paper, module width is the first thing to check, not the checksum.

Generating the symbol instead of drawing it

The reason to compute the checksum in code rather than by hand is that hand calculations do not survive a change of data. If the barcode comes from a spreadsheet column, the value changes every row and the symbol has to be regenerated for each one.

ZebraSheet is a Windows app I built that does this from a sheet: you map a column to a barcode field, pick Code 128, Code 39, EAN-13, UPC-A, QR or Data Matrix, and it generates the symbol and the ZPL for each row, with a preview so a bad symbol is visible before it reaches the printer.

How to test a barcode properly

Scan it with a real scanner, not a phone camera app, and check the decoded string against the source value character by character. A scanner that beeps but returns a truncated string is a symbology setting, not a checksum failure. And print the test on the actual label stock: a barcode that passes on paper can still fail on glossy stock at the same size.

Code 128 is a small specification with one arithmetic rule at its centre. Get the start value, the position weights and the modulo right, leave the quiet zone alone, and the symbol will scan.