Try editing the text below to see how the barcode changes!

You can check out the Wikipedia page for Code 128 for the details, but here's the basics.

Each character is represented as a particular sequence of bars and spaces. The barcode always starts with a START sequence, and ends with the STOP sequence.

For error detection, there's a checksum just before the STOP sequence.

There are actually three different ways of encoding data into the barcode, but this page only supports two: Code B and Code C. Code C is used for encoding numbers, and Code B is used for encoding text. Try typing several numbers in a row to see it switching between encodings. Code A is used for encoding special characters.

Some more thoughts

This page is inspired by Float Exposed, a similar tool for floating point numbers, as well as Mess With DNS.

I don't think the specification prescribes when to switch between the different encodings. Here, if there's 4 digits in a row, it switches to Code C, but that's not always going to be optimal. Wikipedia suggests using dynamic programming to find the optimal encoding. Neat!

The specification for the format is defined in ISO/IEC 15417:2007, which you have to pay to read. That's a shame.

This page was partly built as an experiment with using Cursor, an AI tool. It's ok. I liked that it supports using Claude. I think I'll use it for projects like this where I'm throwing together something quickly.