Home
Practical Code 128 Barcode Samples and Encoding Logic
Code 128 is a highly efficient, high-density linear barcode symbology capable of encoding the entire 128-character ASCII set. Since its introduction, it has become the standard for logistics, healthcare, and inventory management due to its compact size and robust error-checking capabilities. Unlike simpler symbologies like Code 39, Code 128 utilizes multiple character sets and a sophisticated checksum algorithm to maintain data integrity in high-speed scanning environments.
The Structural Anatomy of a Code 128 Barcode
Every Code 128 barcode follows a strict physical structure consisting of six distinct sections. Understanding these sections is essential for anyone developing custom label software or troubleshooting scanning issues.
1. The Quiet Zone
The quiet zone is the blank space at both ends of the barcode. For Code 128, the industry standard requires a quiet zone at least ten times the width of the narrowest bar (the "X-dimension"). Without a sufficient quiet zone, scanners cannot distinguish the barcode from other graphics or text on the label.
2. The Start Character
The start character tells the scanner which initial character set is being used. There are three specific start characters:
- Start A: Initiates encoding in Subset A (Alphanumeric and Control characters).
- Start B: Initiates encoding in Subset B (Alphanumeric and Lowercase characters).
- Start C: Initiates encoding in Subset C (Numeric pairs).
3. Encoded Data
This section contains the actual information. Code 128 is unique because it can switch between character sets mid-barcode to optimize space. For instance, a barcode might start in Subset B for text and switch to Subset C for a long sequence of serial numbers.
4. The Check Character (Check Digit)
Code 128 requires a mandatory check digit calculated using a Modulo 103 algorithm. This digit is not represented in the human-readable text but is vital for the scanner to verify that the data has been read correctly.
5. The Stop Character
A specific pattern of bars and spaces that signals the end of the symbol.
6. The Termination Bar
A final bar (usually two units wide) that follows the stop character, marking the absolute edge of the encoded symbol.
Detailed Breakdown of Subsets A, B, and C
The power of Code 128 lies in its versatility. By utilizing three different character sets, it achieves high density for both text and numbers.
Subset A: Control Characters and Uppercase
Subset A is primarily used in industrial environments where control characters (like NUL, SOH, STX, ETX) are required. It includes:
- All standard alphanumeric uppercase letters.
- Punctuation marks.
- ASCII control characters (00 to 31).
Subset B: Standard Alphanumeric and Lowercase
Subset B is the most common choice for general purpose labeling. It includes:
- All standard alphanumeric uppercase and lowercase letters.
- Punctuation marks.
- Special characters.
Subset C: Numeric Pairs (Double Density)
Subset C is designed specifically for numbers. It encodes two digits into a single barcode character. For example, the digits "12" are not encoded as "1" and "2" separately; they are encoded as the value 12. This makes Subset C twice as efficient as A or B for numeric data. If you are printing a shipping label with a 20-digit tracking number, using Subset C is non-negotiable for space efficiency.
Modulo 103 Checksum Calculation Sample
Calculating the checksum is the most complex part of implementing Code 128. Let’s walk through a practical sample using the data string: "Ref 123".
To encode this, we will use Start B because it contains lowercase letters.
Step 1: Assign Values to Characters
Referencing the standard Code 128 table:
- Start B: Value = 104
- 'R': Value = 50
- 'e': Value = 69
- 'f': Value = 70
- ' ' (space): Value = 0
- '1': Value = 17
- '2': Value = 18
- '3': Value = 19
Step 2: Apply Weights
The weight for the Start character is always 1. For each subsequent data character, the weight increases by 1 (Position 1 gets weight 1, Position 2 gets weight 2, and so on).
- Start B: 104 × 1 = 104
- 'R' (Pos 1): 50 × 1 = 50
- 'e' (Pos 2): 69 × 2 = 138
- 'f' (Pos 3): 70 × 3 = 210
- ' ' (Pos 4): 0 × 4 = 0
- '1' (Pos 5): 17 × 5 = 85
- '2' (Pos 6): 18 × 6 = 108
- '3' (Pos 7): 19 × 7 = 133
Step 3: Sum the Weighted Values
Total Sum = 104 + 50 + 138 + 210 + 0 + 85 + 108 + 133 = 828
Step 4: Calculate Modulo 103
Divide the total sum by 103 and find the remainder. 828 ÷ 103 = 8 with a remainder of 4 (because 103 × 8 = 824).
Step 5: Determine the Check Character
The remainder (4) corresponds to the character with value 4 in the Code 128 table, which is the "$" symbol.
The final string to be converted into bar patterns would be: [Start B] Ref 123 [Check Character Value 4] [Stop].
ZPL Programming Samples for Zebra Printers
Zebra Programming Language (ZPL) is the primary method for generating barcodes on industrial thermal printers. The command for Code 128 is ^BC.
Basic Sample (Subset B)
This code prints a simple Code 128 barcode for the string "PART1234".
-
Topic: BC (Code 128)https://www.zebra.com/content/dam/external-dam/knowledge-base/000014078/Code%20128.pdf
-
Topic: 条码 规范 — — code 128 _ esc 条码 在线 设计 - csdn 博客https://blog.csdn.net/kzhuangood/article/details/72953356
-
Topic: Initial commit: Code 128 barcode encoder for Google Sheets · douglsmith/code128-barcode-google-sheets@cc5a973 · GitHubhttps://github.com/douglsmith/code128-barcode-google-sheets/commit/cc5a97371fee2f81e542047c0eeb82dc5ffcc7ca