Code 128 Barcode Explained
Code 128 was developed by Computer Identics in 1981 and has become very popular over the years. A Code 128 barcode symbol can encode letters, digits and control characters (TAB, ENTER etc.) which makes it very versatile and suitable for many applications. Today, Code 128 is one of the most widely used barcode types, for example for warehouse management, in the transport industry (e.g. by UPS or DHL) and in retail.
Code GS1-128 is an application of Code 128 which encodes a GTIN-12, GTIN-13 or GTIN-14 plus supplemental data like best-before dates or batch numbers. See Code GS1-128 for a discussion of GS1-128 but read this page first.
Code 128 at a Glance
Code 128 Specifications
Code 128 is standardized, the respective document is ISO/IEC 15417:2007, which can be obtained from ISO. As the code is rather old, the Code 128 specs are freely available online, for example in the Wikipedia article on Code 128.
Code 128 code sets A, B, C and start charactersThree different code sets or sub types are defined for Code 128 (namely Code 128 A, Code 128 B, and Code 128 C) that determine how the code is interpreted by the barcode scanner. The code sets differ in compactness and encodable character set.
Code 128 A: Partial ASCII set, no lower case, but ASCII control chars (TAB, CR/LF etc.)
Code 128 B: Full ASCII set, no ASCII control chars
Code 128 C: Only digits 0-9, encoded in pairs, very compact code
The code set to use is indicated to the scanner by the start symbol or start character. Also available are various mode switching or shift characters to switch from one set to another within a barcode symbol. Usually the sub type selection and switching within a symbol is handled by the barcode software that creates the code.
For reference we are including the bar patterns of the three start- and the single stop-character here. When using a Code 128 barcode font in Excel or similar applications, the start and stop characters will be mapped to certain character positions; however, the mapping is not standardized and will vary from font to font.
Code 128 A start character (bitpattern/widths: 211412, bar-gap-bar-gap-bar-gap, widths add up to 11):
Code 128 B start character (widths: 211214):
Code 128 C start character (widths: 211232):
Code 128 stop character (widths: 311123):
Code 128 Auto
Code 128 Auto refers to the automatic selection of the 128 subtype depending on the encoded data. This is performed by the respective barcode software to minimize the size of the code. Code 128 Auto is not a separate version or subset of the code.
Accented, diacritic characters, emoji with Code 128In principle non-ASCII characters like German umlauts (e.g. "Ä Ö Ü") or accented characters (e.g. "á ñ ò") can be encoded in a Code 128 symbol by using a special character (FNC4), defined for Code 128 A and Code 128 B. However, this feature is not widely supported and cumbersome to use. Using a 2D barcode symbology like QR or Data Matrix with dedicated support for non-ASCII or Unicode data might be a better choice.
Code 128 maximum length
Code 128 is a variable length code, which in principle can encode an arbitrary length of data. The practical content limit for Code 128 is at 30 characters (large cap and small cap letters) or a maximum of 60 digits (purely numeric code).
Code 128 B with 30 characters, module width 0.3mm, total length c. 135mm (click for full size):
Code 128 C with 60 digits, module width 0.3mm, total length c. 135mm (click for full size):
Note that many barcode scanners or reader apps have soft limits below those numbers. Also, a barcode must not be wider than the "reading window" or aperture of a scanner or it won't be scanned.
Code 128 bit pattern
Please see this post for the Code 128 bit pattern.
Check digit calculation for Code 128
Code 128 uses a Modulo 103 checksum algorithm. All characters of a symbol are added up and modulo divided by 103. The remainder is the checksum. For a sample calculation, see the Wikipedia article on Code 128.
The barcode software that generates the code will usually calculate the check digit automatically. The check digit can be any character from the code set, including non-printable characters like TAB or EOT. It is therefore not common to append the check character to the readable text under the code, likewise it is usually not transmitted by the scanner with the encoded data (check your scanner's manual for settings regarding Code 128).
In principle, Code 128 can be created without a check digit. However, all recent barcode scanners or scanner apps will expect a symbol to carry a check digit. This has implications when using a Code 128 barcode font, for example in Excel or Google Sheets, see below.
Sample Code 128Purely numeric data, resulting in a Code 128 C symbol (compare the first three bars to the start characters listed above):
Mixed data, resulting in a Code 128 B full ASCII symbol. Also note that this code is wider than the previous example although fewer characters are encoded:
Mixed data ("ABC<TAB>DEF>"), resulting in a symbol that starts as Code 128 B to encode "ABC", then shifts to Code 128 A to encode the <TAB>, then back to 128 B to encode "DEF":
Note: The TAB is not shown in the textline, same goes for other non-printable characters.
When scanning this code, the barcode scanner will put out "ABC", followed by the <TAB>, then "DEF". This can be very useful: Consider a data entry mask with two text fields. The <TAB> can be used to move the input focus from one field to the next. The first part of the code ("ABC") would go into the first field, the second part ("DEF") would go into the second field.
The result can also be seen when decoding the code with Google Zxing, here the TAB is actually expanded:
Non printable characters (like the <TAB> above) are entered into our Code 128 generators using the tilde notation: You enter the tilde, followed by the three digit ASCII code of the symbol. E.g. to encode a <TAB> into the code you would enter "~009". For the code above the full entry string would read: "ABC~009DEF".
Code 128 vs Code 39
Another popular choice to encode alphanumeric characters is Code 39. The biggest advantages of Code 39 is that it can be used without a check digit. On the negative side, the barcode will only natively encode capital characters ("ABC"); small caps ("abc") can be encoded with Code 39 Extended but this code can get very large, as the following example illustrates (Code 128 top, Code 39 bottom):
Likewise, Code 39 does not support compact numerical symbols like Code 128 C and is generally a bad choice for larger amounts of data. For more info see Code 128 vs Code 39.
Code 128 vs UPC / EAN
UPC and EAN will only encode numbers while Code 128 can also encode letters (large cap and small cap) and a variety of special characters. In addition, EAN and UPC are fixed length codes: They only encode exactly twelve (UPC-A, GTIN-12) or thirteen (EAN 13, GTIN-13) digits. For more about EAN, see EAN 13 Explained.
Amazon FBA Barcodes (ASIN, FNSKU)
With FBA (Fulfillment by Amazon), two different types of barcodes are used:
Code 128 font for Excel or Google Sheets
Code 128 is a good choice for embedding barcodes in your spreadsheet. Applications include catalogs with article numbers, serial numbers or GS1 data points like GTIN or best-before dates. The easiest and most straight forward way of creating Code 128 barcodes in Excel or Google Sheets is with a Code 128 barcode font. In order to support the different subtypes of Code 128 and the mandatory check digit, such a barcode font can only be used in conjunction with an add-in, for example the Softmatic barcode add-in for Excel.
This screenshot shows the add-in panel with a column of cells containing sequential Code 128:
Note, that it is not sufficient to simply select the cell with your data and change the font to "Code 128" (a common misconception with the Libre Code 128 barcode font). This will not create a valid barcode because start-/stop-characters and the mandatory check digit are missing. Usually a Code 128 font for Excel will come with an add-in or macro to generate the required start and stop characters and the check digit. The macro or add-in will also format the data for the most appropriate Code 128 subtype.
Code 128 Decoder
Manually decoding Code 128 is possible, beginning with the start symbol and going forward one character or pair of digits at a time. A Code 128 decoder will also have to take mode switching and shift characters into account. Finally, the validity of the decoded data has to be verified against the Modulo 103 check digit. A reference decoding algorithm can be found in the GS1 General Specifictations, section 5.4.5 (download from GS1).
It's much more convenient though to use an app or an online service:
Image shows the output for the above sample of Code 128 C (encoded data: "1234567890"), using Google Zxing, a free online decoder for Code 128. The "Raw bytes" section of the decoded result consists of eight bytes. The first and last bytes are the start and stop characters. In between are five bytes with the actual data (recall that Code 128 C encodes pairs of characters, i.e. "12", "34", "56" etc.) and the checkdigit.
If you want to try for yourself, here is a 300dpi version of the sample code.
It is safe to assume that all barcode scanners available today will read Code 128 barcodes. However, there are some pitfalls and caveats when scanning Code 128; see here if you have a Code 128 symbol that can not be scanned.
Create Code 128 in Illustrator, InDesign
Easy to use barcode plug-ins and extensions for Adobe InDesign, Illustrator und Photoshop. For more videos, see the Softmatic Youtube Channel
Code 128 GeneratorSoftmatic BarcodePlus V5 creates Code 128 and will export the barcode as PDF / SVG (resolution-independent vector) oder raster image (PNG, TIFF). The app will automatically select the appropriate 128 subtype:
Create Code 128 in bulk
For batch creation of Code 128 and Code 128 GS1, use Softmatic BarcodeFactory V5. The software will create thousands of barcode symbols in seconds, with data coming from the built-in serial number generator, the clipboard or via import from Excel, Numbers or CSV text files: