User Rating 0.0
Total Usage 0 times
Supports ASCII 0–127. Auto mode optimizes subset switching.
Is this tool helpful?

Your feedback helps us improve.

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.

code 128 barcode generator barcode code 128 encoder barcode maker 1d barcode

Formulas

The Code 128 check digit ensures scan integrity. It is computed as a weighted positional sum modulo 103:

C = ( Vstart + ni=1 i Vi ) mod 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

ValueCode ACode BCode CBar PatternWidths (B-S-B-S-B-S)
0SPSP00██ █ ██ ██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
160016█ ███ ██ █1-3-2-2-2-1
32@@32█ █ ███ ██1-1-3-1-2-3
64NUL`64███ █ █ ██3-1-1-1-2-3
95USDEL95███ ██ █ █3-2-2-1-1-2
96FNC 3FNC 396██ █ █ ███2-1-1-1-3-3
97FNC 2FNC 297██ █ ███ █2-1-1-3-3-1
98SHIFT BSHIFT A98██ ███ █ █2-3-3-1-1-1
99CODE CCODE C99██ █ ███ █2-1-3-3-1-1
100CODE BFNC 4CODE B██ █ █ ███2-1-1-1-1-3 (variant)
101FNC 4CODE ACODE A███ █ ██ █3-1-1-2-2-1 (variant)
102FNC 1FNC 1FNC 1██ ██ █ ██2-2-1-1-3-2 (variant)
103START A██ █ █ ████2-1-1-4-1-2
104START B██ █ ████ █2-1-1-2-1-4
105START C██ █ ██ ████2-1-1-2-3-2
106STOP██ ███ █ █ ██2-3-3-1-1-1-2

Frequently Asked Questions

Use Code A when your data contains ASCII control characters (values 0 - 31) such as TAB, CR, or LF that appear in serial communication protocols. Use Code B for general printable text including lowercase letters (ASCII 32 - 127), which covers most product labeling. Use Code C exclusively for even-length numeric strings - it encodes digit pairs into single symbols, halving barcode width. Auto mode handles mixed content by switching subsets mid-stream using CODE A/B/C shift symbols.
Code C requires digit pairs. If the input has an odd length, the last digit cannot form a pair. In strict Code C mode, this tool will flag the error. In Auto mode, the algorithm encodes the maximum even-length prefix in Code C, then switches to Code B for the remaining single digit. This adds one switch symbol plus one data symbol rather than failing.
The positional weighting (multiplying each symbol value by its 1-based index) means that transposing two adjacent symbols changes the weighted sum. A simple unweighted sum would miss transpositions where Vi + Vj = Vj + Vi. With weights, i Vi + j Vj j Vi + i Vj (unless Vi = Vj). The modulus 103 (a prime) maximizes detection of single-symbol substitution errors.
ISO/IEC 15417 specifies a minimum quiet zone of 10 times the module width (X dimension) on each side. This tool defaults to 10 modules. If the barcode is printed on a colored or busy background, increase to 20 modules. Insufficient quiet zones cause scanners to fail to detect the start/stop patterns, particularly with CCD-based readers.
Yes. Code A covers ASCII 0 - 95 (NUL through US plus digits and uppercase). Code B covers ASCII 32 - 127 (space through DEL plus lowercase). Together they span the full 128 ASCII characters. Characters below 32 require Code A; lowercase letters and characters 96 - 126 require Code B. The SHIFT symbol allows temporarily borrowing one character from the other subset without a permanent switch.
There is no specification-defined maximum length. The practical limit is scanner field width divided by module width. At a typical X dimension of 0.25 mm and a 100 mm print width, approximately 35 Code B characters or 70 Code C digits fit. Wider barcodes increase the risk of label curvature causing read failures. For data exceeding 48 characters, consider GS1-128 segmentation or switching to a 2D symbology.