Blog
9 min readMoreFreeTools Team

How Barcodes Encode Data: From Grocery Store Stripes to QR Codes

Learn how barcodes actually encode data using binary patterns, check digits, and error correction. Covers EAN-13, UPC-A, Code 128, Code 39, and QR codes.

how barcodes workbarcode generatorEAN-13 barcodehow to create a barcodebarcode encoding explained

Barcode Generator

The free tool referenced in this article — try it directly in your browser.

Open the tool

Every time a cashier scans your groceries, a laser is reading a precise mathematical pattern encoded in black and white lines. But how do barcodes actually encode data? The answer is more elegant than most people realize, and understanding it reveals why barcodes are one of the most reliable data systems ever built. This guide explains the actual encoding mechanics behind every major barcode format, from the stripes on a cereal box to the QR code on your boarding pass.

A Brief History: From Patent to Grocery Store

The story begins with Norman Woodland and Bernard Silver, who filed US patent 2612994 in 1949. Woodland, inspired by Morse code, extended dots and dashes into lines of varying widths. The technology sat mostly dormant for decades until laser scanning became practical.

The first commercial barcode scan happened on June 26, 1974, in Troy, Ohio, when a pack of Wrigley's Juicy Fruit gum was scanned at a Marsh supermarket. That single scan proved the system worked at retail speed, and the modern supply chain was born.

Today, GS1, the global standards organization, manages barcode standards used in over 150 countries. Roughly 6 billion barcodes are scanned every day worldwide.

How 1D Barcodes Encode Information

Traditional one-dimensional barcodes encode data through the widths of alternating bars and spaces. The key insight is that a scanner does not read color: it reads transitions between dark and light, and measures how wide each element is.

Most modern 1D barcode systems use a module-based encoding approach. A "module" is the narrowest possible bar or space in a given symbol. Wider elements are exact multiples of that module width. The scanner measures element widths as multiples of the module and converts them into a binary string, then decodes that string against a lookup table defined by the barcode standard.

EAN-13: The Global Retail Standard

EAN-13 (European Article Number, 13 digits) is the barcode on almost every consumer product sold outside North America. Its structure is:

Position Digits Meaning
1-3 Country/region prefix GS1 prefix (e.g., 590 = Poland)
4-8 Manufacturer code Assigned by GS1 to the company
9-12 Product code Assigned by the manufacturer
13 Check digit Calculated from the first 12

Each digit is encoded as a pattern of 2 bars and 2 spaces spanning exactly 7 modules. The left and right halves of the barcode use different encoding tables, which is how the scanner knows which direction it is reading.

Check digit calculation for EAN-13: The check digit prevents misreads. For the 12-digit string 590123412345:

  1. Take odd-position digits (positions 1, 3, 5, 7, 9, 11): 5, 0, 2, 4, 2, 4. Sum = 17.
  2. Take even-position digits (positions 2, 4, 6, 8, 10, 12): 9, 1, 3, 1, 3, 5. Multiply each by 3, sum = 66.
  3. Total = 17 + 66 = 83.
  4. Check digit = next multiple of 10 minus total = 90 - 83 = 7.

The complete EAN-13 is 5901234123457.

UPC-A: The North American Standard

UPC-A is the 12-digit barcode used in the United States and Canada. It is essentially EAN-13 with a leading zero implied. Its structure:

  • Digit 1: Number system digit (0 = standard grocery, 2 = variable-weight produce, 3 = drugs and health items, 5 = coupons)
  • Digits 2-6: Manufacturer code (5 digits, assigned by GS1 US)
  • Digits 7-11: Product code (5 digits, assigned by manufacturer)
  • Digit 12: Check digit (same alternating 1/3 weight formula as EAN-13)

A UPC-A barcode encodes 12 digits across 95 modules using start, center, and end guard patterns.

Code 39: The Original Alphanumeric Barcode

Code 39 was invented in 1974, the same year as the first retail barcode scan. It was the first barcode to encode letters as well as numbers, making it the standard for industrial, military, and healthcare applications for many years.

The encoding logic is built into the name: each character is represented by 9 elements (5 bars and 4 spaces), and exactly 3 of those 9 elements are wide. "Three of nine" gives Code 39 its alternative name. Wide elements represent 1, narrow elements represent 0, giving each character a unique 9-bit pattern.

Code 39 natively supports uppercase letters A-Z, digits 0-9, and the symbols: space, $, %, +, -, ., /. It includes a start and stop character (asterisk, *) that the scanner recognizes as delimiters.

Code 128: High-Density ASCII Encoding

Code 128 is a much denser barcode format that can encode all 128 ASCII characters. Each character is represented by 3 bars and 3 spaces with varying widths, and the total width of any character is always exactly 11 modules. This fixed-width property makes scanning highly reliable.

Code 128 has three subtypes:

Subtype Encodes
Code 128A Uppercase letters, digits, control characters
Code 128B Upper and lowercase letters, digits, special characters
Code 128C Numeric pairs only (digit density doubles)

Code 128 includes a check character calculated as: (start symbol value + sum of each symbol value multiplied by its position) mod 103. This is a more sophisticated integrity check than EAN-13's simple check digit.

Related Tool

Barcode Generator

Use it directly in your browser. No sign up, no download, no data stored.

Use the Tool

QR Codes: 2D Barcodes and the Power of Error Correction

QR codes (Quick Response codes) were invented by Denso Wave in 1994 to track automobile parts during manufacturing. Unlike 1D barcodes that encode data in one axis, QR codes encode data in a 2D matrix of black and white squares.

QR codes can hold dramatically more data than 1D barcodes:

Data type Maximum capacity
Numeric only 7,089 characters
Alphanumeric 4,296 characters
Binary (bytes) 2,953 bytes
Kanji characters 1,817 characters

How QR Codes Store Data

A QR code is divided into functional regions. The three large squares in three corners are finder patterns, which let the scanner locate and orient the code regardless of rotation or angle. The smaller squares are alignment patterns used for distortion correction.

Data is encoded in the remaining cells using Reed-Solomon error correction. The encoded data is split into data codewords and error correction codewords. These codewords are placed in the matrix following a specific zigzag path, then a mask pattern is applied to prevent large uniform areas that would confuse scanners.

Reed-Solomon Error Correction: Why QR Codes Survive Damage

This is the feature that makes QR codes remarkable. Reed-Solomon is a mathematical algorithm that adds redundancy data alongside the original data. If part of the QR code is damaged, obscured, or missing, the redundancy data allows the scanner to reconstruct the original message.

QR codes support four error correction levels:

Level Data restored Typical use
L (Low) ~7% Clean environments
M (Medium) ~15% General purpose
Q (Quartile) ~25% Industrial/dirty environments
H (High) ~30% When a logo will be added

This is exactly why you can put a company logo in the center of a QR code and it still scans: the logo covers some cells, but the Reed-Solomon redundancy allows the scanner to reconstruct the missing data. Designers typically use Level H error correction when embedding logos.

Format Comparison

Format Type Max characters Error correction Common use
EAN-13 1D 13 digits Check digit Retail products globally
UPC-A 1D 12 digits Check digit Retail products in North America
Code 39 1D Variable Optional check digit Industrial, military, healthcare
Code 128 1D Variable (all ASCII) Check character Shipping, logistics, GS1-128
QR Code 2D Up to 7,089 numeric Reed-Solomon (7-30%) URLs, payments, tickets

Frequently Asked Questions

Why does every barcode have a check digit?

A check digit is a mathematically derived digit appended to the barcode data. When a scanner reads the barcode, it recalculates the check digit from the data it read and compares it to the printed check digit. If they do not match, the scanner rejects the read and tries again. This catches the most common scanning error: misreading a single digit due to a smudged bar or scanner noise.

What is the difference between EAN-13 and UPC-A?

UPC-A is a 12-digit system developed in the United States in 1974. EAN-13 is a 13-digit system developed in Europe in 1976 as a superset of UPC-A. Every UPC-A barcode can be converted to EAN-13 by prepending a zero (so a UPC-A starting with 01234 becomes EAN-13 0012340...). Modern scanners read both formats. GS1, the global standards body, now manages both under a unified framework.

Why can QR codes have logos?

QR codes can have logos because Reed-Solomon error correction adds redundant data to the code. When a logo covers part of the QR code's cells, those covered cells are treated as erasures. The Reed-Solomon algorithm can reconstruct erased data as long as the total damage does not exceed the error correction level. Using Level H (30% restoration), a designer can safely cover up to about 30% of the QR code's area with a logo.

What does Code 128 stand for?

"Code 128" refers to the fact that the standard encodes the full 128-character ASCII set. The name describes the character set scope, not the number of bars or modules. Code 128 was developed by Computer Identics Corporation in 1981 and became one of the most widely used linear barcode formats because of its density and full ASCII coverage.


Ready to generate your own barcode? Use the Barcode Generator to create EAN-13, UPC-A, Code 128, Code 39, or QR codes instantly, with no software to install.

Related Tool

Barcode Generator

Use it directly in your browser. No sign up, no download, no data stored.

Use the Tool