Monday, September 10, 2018

Counting bars in Code 128

I was trying to decode a generated barcode by hand and it was just too hard. So why not use my favorite new language, darkbasic classic.

I load the bitmap and then just display the bar widths and manually look it up in the table at https://en.wikipedia.org/wiki/Code_128.

Quick and dirty but so much easier than trying to count pixels by hand...








So why only 107 symbols in Code 128?

Sometimes you just wonder why things have a certain number of possibilities. Like code 128 barcodes. Why does it have a certain # of symbols?

From https://en.wikipedia.org/wiki/Code_128

It's got a constant 11 bit width with 3 bars and 3 spaces. Each bar can be from 1 to 4 units thick.

So you have 6 bars and they can have 4 possibilities each.

You'd think it would be 4 * 4 * 4 * 4 * 4 = 4096 possibilities, but the number of units all has to add up to 11.

Let's write some javascript to do some counting for us:




and it dutifully gives us 216 possibilities.

If we remove half of those 216 taking away the reverse bit patterns, we get 108 which is pretty close to the 107 symbols that you can have in code 128.