TC
image6 min read

QR Codes: How They Store Data in Squares

A QR code packs up to 4,296 characters into a tiny grid of black and white squares. But how does a camera decode those squares back into a URL or a Wi-Fi password? The answer involves clever binary encoding, built-in error correction, and a layout designed so scanners can read codes at any angle — even when partially damaged.


Anatomy of a QR code

Every QR code contains several structural elements that help scanners locate and orient the code before reading any data:

  1. Finder patterns — The three large squares in the corners (top-left, top-right, bottom-left). These tell the scanner “a QR code starts here” and establish its orientation. The ratio of black-white-black widths (1:1:3:1:1) is unique enough to be detected at any angle.
  2. Timing patterns — Alternating black and white modules running between the finder patterns. These help the scanner determine the exact grid spacing.
  3. Alignment patterns — Smaller squares placed in larger QR codes to correct for perspective distortion (like scanning at an angle).
  4. Format information — Two strips near the finder patterns that encode the error correction level and mask pattern.
  5. Data modules — Everything else — the actual encoded information, laid out in a specific zigzag pattern.
QR code structure (simplified):

┌─────────┐     ┌─────────┐
│ ███████ │ ─── │ ███████ │   ← Finder patterns
│ █     █ │     │ █     █ │
│ █ ███ █ │     │ █ ███ █ │
│ █     █ │     │ █     █ │
│ ███████ │     │ ███████ │
├─────────┤     └────┬────┘
│ Timing  │──────────│      ← Timing pattern
├─────────┤          │
│         │  Data    │
│ ███████ │  modules │        ← Data fills remaining space
│ █     █ │          │
│ █ ███ █ │   ┌──┐   │
│ █     █ │   │██│   │        ← Alignment pattern
│ ███████ │   └──┘   │
└─────────┴──────────┘

How data is encoded

QR codes support four encoding modes, each optimized for different content types:

ModeCharactersBits per charMax capacity
Numeric0-93.37,089 digits
Alphanumeric0-9, A-Z, space, $%*+-./:5.54,296 chars
ByteAny (UTF-8)82,953 bytes
KanjiJapanese characters131,817 chars

The QR encoder automatically selects the most efficient mode for the input data. A URL like https://example.com gets encoded in byte mode (8 bits per character), while a phone number uses numeric mode (just 3.3 bits per digit). This is why QR codes for short numbers are noticeably smaller than codes for long URLs.


Error correction: surviving damage

One of QR's most impressive features is Reed-Solomon error correction. The encoded data includes redundant codewords that let the scanner reconstruct missing or damaged portions — even if part of the QR code is torn, stained, or obscured.

Four error correction levels

LevelRecoveryOverheadBest For
L~7%LowClean digital displays
M~15%MediumGeneral purpose (default)
Q~25%HighPrinted materials
H~30%HighestHarsh environments, logos
Why logos work in QR codes: When you see a QR code with a company logo in the center, it's using level H error correction. The logo deliberately destroys up to 30% of the data modules, but the error correction recovers the full message anyway. This only works because the redundancy overhead is built in.

Why QR codes are always square

QR codes use a square grid because the finder patterns need to be detectable from any rotation. The three corner squares create a unique geometric relationship that cameras can identify instantly — they determine position, size, and angle in a single scan pass. A rectangular code would need different detection logic for landscape vs. portrait orientation.

The “version” of a QR code determines its size. Version 1 is 21×21 modules, and each version adds 4 modules per side, up to Version 40 at 177×177 modules. Higher versions hold more data but require higher-resolution printing and scanning.


Practical encoding tips

  • Shorter URLs = smaller codes — Use URL shorteners if your QR code needs to be printed small.
  • Error correction M is the safe default — It balances density and reliability. Only use H if you need a logo overlay.
  • Dark foreground on light background — Scanners expect this contrast pattern. Inverted codes work but scan less reliably.
  • Maintain a quiet zone — Leave white space (at least 4 modules wide) around the QR code to prevent scanner confusion.
QR codes survived a decade of “that'll never catch on” skepticism because the engineering is genuinely brilliant: self-orienting, self-correcting, and backwards compatible from Version 1 to Version 40.

Try it yourself

Put what you learned into practice with our QR Code Generator.