We covered the Code 39 barcode symbology in a previous chapter. Now we’ll tackle the barcode symbology for Code 39’s little brother, Code 128.
Code 128 is an improvement on Code 39. It was designed to be more compact than Code 39 and it is about 30% narrower. This means more data can be encoded in a smaller space. It also means spending less money on ink or toner.
Here is the string 1234567 encoded using Code 128.
Here is the same string 1234567 encoded with Code 39:
You can see it takes a lot more bars to encode the Code 39 version. Code 128 is more efficient.
Everyone loves efficiency, BUT creating Code 128 barcodes is more complicated than creating Code 39 barcodes.
In keeping with the theme of this book, we'll try not to get bogged down in the complications. Instead, we'll narrow our scope to just what you need to know to get the job done.
Characters and Code Sets
Code 128 supports the full ASCII character set, which includes numbers (0-9), both upper- and lower-case letters (a-z and A-Z) and punctuation.
However, you might need to be aware that it supports all these characters through a potentially-confusing concept of "code sets". I say "you might need to be aware" because for asset tracking applications where we're encoding relatively simple values into our barcodes, whatever tool you use to create Code 128 barcodes is going to take care of the complexity for you.
Here’s just enough detail about barcode symbology and sets to get by. You can learn the rest if needed by Googling "code 128".
Code 128 has three code sets named A, B and C. Each code set can only encode some of the characters. You can switch between the code sets "on the fly" in the middle of a barcode, if needed, to access all the available characters. But for most simple cases involving numbers and upper- and lower-case letters, you can just stick with code set B.
If you need to encode the LATIN-1 (ISO-8859-1) characters, there's a special operator called Function 4 (FNC4). This allows for the encoding of the high (128-255) character codes. This could be useful if the data you need to represent requires those characters. But before you begin using FNC4 in earnest, make sure your barcode readers can read them, as not all do.
If You're Into Micro-Optimization
The code sets have some overlap — they contain some of the same characters. For example, both code set A and B contain all the numbers, the uppercase English characters and some other special characters. Code set B has the lower-case letters.
Since there's overlap between code sets, it's interesting to know that you can encode the exact same value in many different ways using combinations of the code sets, producing valid barcodes of differing widths. If you really needed to minimize the size of the barcode, you might explore available algorithms that can produce the narrowest possible Code 128 barcode. Again, most of us will just stick with code set B.
Code 128 has a mandatory checksum character. The checksum is also called "error correction". A checksum is a bit of math that is performed on all the characters being encoded. The calculation produces a single character, which is then tacked onto the end of the barcode. The barcode scanner reads the barcode and recalculates the checksum. If the checksum value the scanner comes up with doesn't match what's in the barcode, the barcode will not scan.
The checksum protects against bad scans. Scanners are imperfect. Sometimes they just read a barcode wrong, especially in poor lighting conditions or when the scanner is moving while scanning.
Checksums are also helpful when humans have to key in the value instead of scanning it. Humans are just terrible at data entry. We often mistype characters, accidentally add characters that aren't there (fat fingers), or transpose two numbers — instead of 1234, we might type 1324.
It is this mandatory checksum requirement that makes it more difficult to create Code 128 barcodes than Code 39 barcodes. Something or someone has to do the math to calculate the checksum. A barcode font isn't going to do this for you.
Code 39 doesn't require checksums. Even so, you should think of a checksum as a requirement regardless of symbology. Using them is just good practice.
Code 128 Fonts
Free Code 128 TrueType fonts exist. Just know you cannot use a font to create Code 128 barcodes unless you also calculate and append the checksum value to every barcode.
There are many barcoding software packages and online web sites that can help you create Code 128 barcodes. Google is your friend here. Try googling "code 128 checksum calculator".
If you want to generate many Code 128 barcodes in batch, you're going to need either an application, a web tool, or a programmer who can write you either a small program or an Excel script to do this. The algorithms, source code and drop-in code libraries are freely available on the Internet.
You can use the same Google Sheets tricks I used in the Code 39 chapter to generate Code 128 barcodes in bulk.
Code 128 is more compact than Code 39, saving both space and ink. Unlike Code 39, it requires a checksum calculation, which makes generating Code 128 barcodes using a barcode font harder. Some avoid Code 128 due to this complexity — but don't! You need those checksums! They are vitally important for avoiding errors and improving accuracy. You should consider them required.
If you’d like more comprehensive information on Code 128 barcode symbology, see this Wikipedia page.