Code 128 (Standard) Generator
Generate Code 128 barcodes (A, B, C, Auto) with check digit calculation, PNG/SVG export, and full character set support. Free online tool.
About
Code 128 is a high-density linear barcode symbology defined in ISO/IEC 15417. It encodes all 128 ASCII characters across three subsets: Code A (control characters + uppercase), Code B (printable ASCII 32 - 127), and Code C (double-density numeric pairs). Each symbol consists of 11 modules arranged as 3 bars and 3 spaces. A mandatory check digit is computed as a weighted sum modulo 103, where weight i corresponds to symbol position. Incorrect encoding produces unreadable labels, halting logistics chains and failing point-of-sale scans. This tool implements the full 106-symbol encoding table with automatic subset optimization to produce the shortest valid barcode.
Auto mode applies a greedy switching algorithm: it selects Code C for runs of ≥4 consecutive digits (or ≥2 at the start), Code A for control characters below ASCII 32, and Code B for all remaining printable text. The tool approximates an optimal encoding path. Edge cases exist: inputs mixing control characters with lowercase letters force subset switches that increase barcode width. The rendered output uses exact module-level pixel mapping on an HTML Canvas for print-accurate results at 300 DPI.
Formulas
The Code 128 check digit ensures scan integrity. It is computed as a weighted positional sum modulo 103:
Where Vstart is the value of the START symbol (103 for A, 104 for B, 105 for C), i is the 1-based position index of each data symbol, Vi is the code value of the symbol at position i, and n is the total count of data symbols (including any CODE A/B/C switch characters). The resulting C maps to one of the 106 symbols and is appended before the STOP pattern.
Auto-optimization selects the shortest encoding. For a digit run of length L at the start, Code C is chosen when L ≥ 2. Mid-string, a switch to Code C occurs when L ≥ 4 (since the CODE C switch symbol costs 1 symbol but saves L2 − 1 symbols). Each encoded symbol produces an 11-module wide pattern, except STOP which is 13 modules. Total barcode width in modules: W = 11 ⋅ (n + 2) + 13 + 2 ⋅ Q, where Q is the quiet zone width (minimum 10 modules per ISO/IEC 15417).
Reference Data
| Value | Code A | Code B | Code C | Bar Pattern | Widths (B-S-B-S-B-S) |
|---|---|---|---|---|---|
| 0 | SP | SP | 00 | ██ █ ██ ██ | 2-1-2-2-2-2 |
| 1 | ! | ! | 01 | ██ ██ █ ██ | 2-2-2-1-2-2 |
| 2 | " | " | 02 | ██ ██ ██ █ | 2-2-2-2-2-1 |
| 3 | # | # | 03 | █ ██ █ ███ | 1-2-1-2-2-3 |
| 4 | $ | $ | 04 | █ ██ ███ █ | 1-2-1-3-2-2 |
| 5 | % | % | 05 | █ ███ █ ██ | 1-3-1-2-2-2 |
| 10 | * | * | 10 | █ █ ██ ███ | 1-1-2-2-3-2 |
| 16 | 0 | 0 | 16 | █ ███ ██ █ | 1-3-2-2-2-1 |
| 32 | @ | @ | 32 | █ █ ███ ██ | 1-1-3-1-2-3 |
| 64 | NUL | ` | 64 | ███ █ █ ██ | 3-1-1-1-2-3 |
| 95 | US | DEL | 95 | ███ ██ █ █ | 3-2-2-1-1-2 |
| 96 | FNC 3 | FNC 3 | 96 | ██ █ █ ███ | 2-1-1-1-3-3 |
| 97 | FNC 2 | FNC 2 | 97 | ██ █ ███ █ | 2-1-1-3-3-1 |
| 98 | SHIFT B | SHIFT A | 98 | ██ ███ █ █ | 2-3-3-1-1-1 |
| 99 | CODE C | CODE C | 99 | ██ █ ███ █ | 2-1-3-3-1-1 |
| 100 | CODE B | FNC 4 | CODE B | ██ █ █ ███ | 2-1-1-1-1-3 (variant) |
| 101 | FNC 4 | CODE A | CODE A | ███ █ ██ █ | 3-1-1-2-2-1 (variant) |
| 102 | FNC 1 | FNC 1 | FNC 1 | ██ ██ █ ██ | 2-2-1-1-3-2 (variant) |
| 103 | START A | ██ █ █ ████ | 2-1-1-4-1-2 | ||
| 104 | START B | ██ █ ████ █ | 2-1-1-2-1-4 | ||
| 105 | START C | ██ █ ██ ████ | 2-1-1-2-3-2 | ||
| 106 | STOP | ██ ███ █ █ ██ | 2-3-3-1-1-1-2 | ||