The Code 39 Barcode Symbology

This is a free preview chapter from the upcoming eBook, The Ultimate Guide to Barcodes for Asset Tracking authored by TrackAbout's Chief Technology Officer, Larry Silverman. If you’d like to be notified when future chapters are shared, please sign up.

  

Demystifying barcode symbologies

What is a Barcode Symbology?

If you’re learning about barcodes, you’ll quickly come across the term “symbology”. The easiest way to think about symbology is that it’s the look and feel of the barcode. A barcode symbology is a specification that describes how a certain barcode looks and how it does what it does.

More than a hundred different symbologies have been developed since the invention of the barcode. Many are obsolete and have been replaced with more robust, readable symbologies. Many aren’t used in typical asset tracking applications. We’ll focus on the barcode symbologies that we see most often in asset tracking software and applications.

We see the following symbologies most frequently in asset tracking applications:

  • Code 39
  • Code 128
  • QR Code
  • Data Matrix

In this chapter, we’ll focus on Code 39.

The Code 39 Symbology

Code 39 has several alternate names: Alpha39, Code 3/9, Type 39, USS Code 39, USD-3 and my favorite, Code 3 of 9, which sounds less like a barcode symbology and more like a Borg designation (perhaps of Tertiary Adjunct of Data Matrix Zero-One. Hey, when you wield a barcode scanner that looks like a phaser, you get to crack the occasional Star Trek joke).

Code 39 is a linear or “1-dimensional” (1D) barcode. A barcode is said to be linear or 1D if it can be read by a sensor that only scans a thin horizontal line (hence linear) that slices perpendicularly across the bars of the barcode. If you recall your geometry, a line has only one dimension (1D). Two intersecting lines define a plane having height and width (two dimensions, or 2D). We’ll discuss 2D barcodes in a future chapter.

Code 39 is notable because although it was invented in 1974, it remains one of the most popular, do-it-yourself barcode types. It is a little limited in that the basic symbology allows encoding just 43 characters. Allowed characters are:

  • Uppercase letters A through Z
  • Numbers 0 through 9
  • 7 special characters – . $ / + % and space.
  • Note the lack of lowercase characters.

The reason for Code 39’s lasting popularity is that it’s very easy to create Code 39 barcodes using free fonts. You can download and install a Code 39 TrueType barcode font from several sites, such as Matthew Welch’s I Shot the Serif font website. Once the font is installed, you can use just about any PC, Mac or Linux word processing or spreadsheet software to create barcodes that work.

If you use the “extended” version of the Free 3 of 9 barcode font, you can use any characters available in the entire ASCII character set including lowercase letters. Be sure to test the barcodes with all your scanner models to ensure they read successfully. Not all scanners may honor the extended character set, so better safe than sorry.

Tutorial: Create a Code 39 Barcode with a Word Processor

Here’s a tutorial on creating a barcode with word processors like Microsoft Word, Apple Pages, OpenOffice or LibreOffice. We’ll use Matthew Welch’s Free 3 of 9 font. We’ll assume you’ve already downloaded the font and installed it in your local operating system.

To encode the number 1234567 as a barcode:

  • Open your word processor and start a new document
  • Type the following, including asterisks:
    *1234567*
  • Highlight it and change the font to “Free 3 of 9”.
  • Increase the font size as needed.
  • Done!

What you get will look like this:

Code 39 Symbology

If you scan the above barcode, you’ll get back the value 1234567. The asterisks will not be included — they only exist to mark the left and right edges (end bars) of the barcode.

Note that there’s no human-readable text included in the barcode. If you intend to print barcodes using this technique, you might want to add text containing the value encoded so humans can read it without having to scan it.

Tutorial: Create a Range of Code 39 Barcodes in Excel

Next I’ll show how you can create a range of barcodes using Excel’s drag-to-extend fill handle feature.

In cell A1 and going down, I create a number sequence. It can be whatever you want. My intent will be to drag the fill handle to grow and extend the number sequence down the column.

In cell B1, I use the CONCATENATE function. CONCATENATE combines text from different cells. Here I use it to place a single asterisk before and after the value from cell A1. If you recall from earlier, these asterisks are required to represent the left and right edges or “end bars” of the barcode. The barcode won’t scan without the end bars.

The function looks like this:
=CONCATENATE("*",A1,"*")

I’m giving the CONCATENATE function three “arguments”. An argument is the value or values you pass into a function. The three arguments are separated by commas. Here I use an asterisk, the value from cell A1, and another asterisk.

Next I highlight both A1 and B1 and drag the fill handle (the small black square in the bottom right of B1) down to extend the sequence.

I change the font of column B to the “Free 3 of 9” font, and we have barcodes!

Increase font size a little and we’re done.

Animated gif showing how to make Code 39 barcodes in Excel

Tutorial: Create a Range of Code 39 Barcodes in Google Sheets

TrueType fonts like our “Free 3 of 9” that have been installed locally on your computer are stuck there on your computer. They’re not available for use in cloud-based word processing or spreadsheet apps like Google Docs and Google Sheets, because those web tools don’t use your local computer fonts.

Instead of a local font, we’re going to use an online barcode generating web service to create our barcodes for us. Don’t panic! I’ll walk you through it.

There are quite a few web sites that can generate barcodes. There will be a whole chapter about them later. For now, I’m going to pick one I know works.

NOTE: Try not to abuse the online barcode generating services. Generating thousands of barcodes using these online services is going to cost someone real money in server processing time and image download bandwidth. Be kind and gentle.

Create a new Google Sheet. Here’s a public Google Spreadsheet you can open to follow along and see the final product.

In column A, start at cell A1 and enter your desired barcode numbers.

In column B, starting in cell B1, we’re going to use the Google Sheets IMAGE function to make a call out to the barcode generation service.

Assuming your first barcode value is in A1, paste the following into B1.

=IMAGE("http://www.barcodesinc.com/generator/image.php?code=" & A1 & "&style=197&type=C39&width=200&height=100&xres=2&font=3", 3)

Now copy or extend cell B1 downward.

Finally, you will likely have to make your rows taller in order to see the barcode images.

What Did I Just Do?

Let’s break down that IMAGE function to better understand what’s going on.

The IMAGE spreadsheet function fetches an image from a web site and displays it in a cell.

Inside the parenthesis, the IMAGE function takes different arguments (or values) separated by commas. These arguments control aspects of how the image is shown.

In our example, we’re only passing two values into the IMAGE function.

  1. A URL or web address to fetch a barcode image
  2. A number that indicates how big we want the image to be when it’s shown in the spreadsheet (3 means “original size”).

The first value we gave the IMAGE function does all the heavy lifting of going out on the Internet and pulling back a barcode image file. Here is that part:

"http://www.barcodesinc.com/generator/image.php?code=" & A1 & "&style=197&type=C39&width=200&height=100&xres=2&font=3"

We are using a common spreadsheet trick to create a new value in B1 by smushing together three things: a string, a value from A1, and another string. The part that looks like & A1 & is pulling the value of A1 into the middle of the URL or web address. Alternatively, we could have used a CONCATENATE function like we did in the Excel example.

If the value in cell A1 was 10000, then the resulting URL becomes:

http://www.barcodesinc.com/generator/image.php?code=10000&style=197&type=C39&width=200&height=100&xres=2&font=3

You could copy/paste that into a browser and you’d get a barcode back.

One of the name/value pairs is code=10000. That tells the web page to encode a barcode for the value 10000.

When reading web addresses like this, it helps to know a few things:

  • The ? character separates the web page on the left from the commands or “name/value pairs” on the right that control how the page behaves.
  • The & character separates different commands or name/value pairs from each other.
  • The = sign is an assignment operator. It assigns a value to a variable. In this case, we’ve assigned the value of 10000 to the variable named code.

Here’s what the other name/value pairs do.

style=197
It’s not entirely clear what the allowable values are, but style encodes four image features: (1) whether the value is printed below the barcode, (2) whether there’s a border, (3) whether text should be stretched and (4) whether the entire image is inverted (white on black). I used the web barcodesinc.com website to get the style the way I liked it, then copied the style= setting from the “Link or embed this barcode” dialog after generating a test barcode.
type=C39
Controls the barcode symbology. C39 will result in a Code 39 barcode being generated. C128B will create a Code 128 Code Set B barcode.
width=200
The image will be 200 pixels wide. This does not change the width of the bars, just the width of the image file generated. If the value you wish to encode is long, you might need a larger number here.
height=100
The image will be 100 pixels tall. Generally, linear barcodes can be as tall as you like.
xres=3
The width of the bars. If you increase this, you may need to increase the value of width to ensure the entire barcode fits in the generated image file.
font=3
Sets the size of the font for the text printed below the barcode, if enabled. Valid values are 1 through 7, with 7 being the biggest.

Code 39 Weaknesses

Code 39 is popular, but it has some serious drawbacks.

Code 39 is Wide
Code 39 barcodes are around 30% wider than the same value encoded with Code 128. As the encoded value gets longer, the Code 39 barcode gets wider, and can become more difficult or even impossible to scan (depending on your barcode scanner’s capabilities). We’ve hit practical limits in the field with very long values encoded using Code 39.

No Error-Correction
The greater weakness is that there is no built-in error correction for Code 39. The Free 3 of 9 font will not add a checksum for you automatically (no font will).

Better symbologies incorporate an algorithmic calculation called a checksum that reduces the odds of a mis-scan. In the case of 1-dimensional or linear barcodes like Code 128, this is usually a single checksum digit placed at the end of the string being encoded. In the case of 2D barcodes like QR and Data Matrix, things get quite a bit more interesting.

Without error correction, you will get bad data. This is a very important point. If you scan the same Code 39 barcode without a checksum 100 times, you might not get the same value every time. A little shake or shimmy when scanning a Code 39 barcode can fool an image scanner into seeing thicker or thinner lines than are really there. That is Really Bad™.

If you still want to use Code 39 (because of its convenience and ease of creation), it’s possible (and recommended) to beef it up by calculating and appending your own checksum digit. This improves accuracy and helps reject bad scans. The Modulo 43 algorithm is often paired with Code 39 for this purpose. Mod 10 (aka the Luhn algorithm) can also be used with Code 39. TrackAbout supports both of these.

You’ll also want to ensure accuracy by having your application support a validation pattern. For example, if you’ll only ever scan barcodes that are all numeric and 7 characters long, then your software had better be able to reject a scan that isn’t 7 characters long or that contains a character other than a number. TrackAbout’s apps support these kinds of validation patterns.

Your app needs to be specially programmed to incorporate validation patterns, to be aware that a checksum has been added and which checksum algorithm was used. The software should also automatically remove that trailing checksum digit before storing the value. Depending on your use case, you probably do not want to record the checksum digit because it’s not significant or important to the data you’re collecting. It’s merely there for error correction. It was generated by an algorithm, and the value can always be calculated again. You lose no information by removing it.

Especially in the case where you are encoding barcode shortcuts like part numbers, you do NOT want that checksum digit coming along for the ride. It is not a part of your catalog number scheme.

Conclusion

We’ve tried to provide a practical primer on Code 39. There’s always more to know, so for comprehensive information on Code 39, see the Code 39 Wikipedia page.

Let’s sum up.

Pros of Code 39

  • Easy to create barcodes with free fonts or online tools

Cons of Code 39

  • 30% wider than Code 128
  • No standard checksum character required or enforced
  • No lowercase characters (unless you use an extended font, and then you might have issues reading them with certain scanners)

This is a free preview chapter from the upcoming eBook, The Ultimate Guide to Barcodes for Asset Tracking authored by TrackAbout's Chief Technology Officer, Larry Silverman. If you’d like to be notified when future chapters are shared, please sign up.