Skip to main content
Back to Blog

How QR Codes Work: Error Correction and Data Density

How QR codes encode URLs and text, what error correction does, and how to generate codes that scan reliably every time.

Kashyap Thakar
8 min
TechnologyQR CodeMobile
How QR Codes Work: Error Correction and Data Density

The Quick Response (QR) code was developed by Denso Wave and announced in 1994, to hold more data than a conventional barcode and be read quickly. The automotive industry was an early user, for tracking parts through production. Today it appears on everything from restaurant menus to billboards.

The Matrix Structure

Unlike a 1D barcode that only stores data horizontally, a QR code is a 2D matrix. This allows it to store significantly more information: at most 7,089 numeric or 4,296 alphanumeric characters, in the largest version (40) at the lowest error correction level.

The three large squares in the corners are "Finder Patterns". They tell the scanner: "Hey, I'm a QR code, and here's my orientation." This is why you can scan a QR code upside down, and it still works instantly.

The Magic of Error Correction

Have you ever seen a QR code with a logo in the middle? How does that not break the code?

QR codes use Reed-Solomon error correction. This adds redundant data to the code, allowing it to remain readable even if part of it is damaged or covered.

  • Level L (Low): Restores ~7% of data. Good for clean, digital codes.
  • Level M (Medium): Restores ~15% of data. A common default in generators.
  • Level Q (Quartile): Restores ~25% of data.
  • Level H (High): Restores ~30% of data. The usual choice if you plan to put a logo in the center or print on a surface that may get scuffed.

Static vs. Dynamic QR Codes

Static codes encode the data directly. If you encode a URL and that URL changes, the printed code is useless.

Dynamic codes encode a short redirection URL. This allows you to change the destination website without reprinting the QR code.

Data Capacity by Version

QR codes come in 40 versions, and each version has more data capacity than the last. These figures are for error correction level L, the lowest:

VersionSize (modules)NumericAlphanumeric
121×214125
1057×57652395
40177×1777,0894,296

How Text Becomes Modules

An encoder first picks a mode, then packs the data into bits, adds Reed-Solomon error correction codewords, interleaves them, and finally applies one of eight mask patterns. The mask is chosen to avoid large same-color areas and false finder-like shapes that confuse scanners, which is why two codes for similar text can look quite different. The mask and error correction level are recorded in the format information, so a scanner knows how to undo it.

ISO/IEC 18004 defines four main data modes, and each packs characters differently:

ModeCharactersPacking
NumericDigits 0-93 digits in 10 bits
Alphanumeric45 characters: 0-9, A-Z (uppercase only), space and $ % * + - . / :2 characters in 11 bits
ByteAny bytes. The standard's default text interpretation is ISO/IEC 8859-1. UTF-8 text is widely used in practice, but a reader may need a hint (an ECI header) to interpret it correctly8 bits per byte
KanjiDouble-byte Japanese characters13 bits per character

In alphanumeric mode each character gets an index in the 45-character set, and two characters are combined as 45 * first + second. For the string AC-42, the pair AC becomes 10 * 45 + 12 = 462 and -4 becomes 41 * 45 + 4 = 1849, computed here in Node.js. A simple mode picker shows why case matters:

const ALNUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
function pickMode(s) {
  if (/^[0-9]*$/.test(s)) return "numeric";
  if ([...s].every((c) => ALNUM.includes(c))) return "alphanumeric";
  return "byte";
}

pickMode("2026")                  // "numeric"
pickMode("HTTPS://EXAMPLE.COM/A") // "alphanumeric"
pickMode("https://example.com/a") // "byte"

A good encoder switches modes within one code to minimize size. Because scheme and hostname are case-insensitive, an uppercase URL can be encoded more densely than a lowercase one, but only use that trick when the path does not depend on case. Also note that numeric-looking data such as order numbers is far cheaper than the same value as text.

Capacity at Version 1

Capacity depends on version and error correction level together. Version 1 has 19, 16, 13 or 9 data codewords at levels L, M, Q and H. Working out how many characters fit after the 4-bit mode indicator and the character count field gives these maximums, which were calculated and cross-checked against the table above:

LevelNumericAlphanumericByte
L412517
M342014
Q271611
H17107

The takeaway is practical. The 24-byte URL https://example.com/menu already does not fit in Version 1 at any level, so it needs a larger grid. The side length is 17 + 4 * version modules (21, 57 and 177 for versions 1, 10 and 40), so every extra character makes modules smaller at a fixed print size. Shorter payloads give bigger modules and faster, more reliable scans.

The Parts of the Symbol, Revisited

  • Quiet zone: the blank margin around the code. The standard calls for 4 modules for QR codes. A tight crop, a border from a neighboring design element or a busy background is a common reason a printed code will not scan.
  • Finder patterns: 7x7 module squares in three corners. There is deliberately no fourth one, and that asymmetry tells the scanner the orientation.
  • Timing patterns: alternating dark and light modules along row 6 and column 6 between the finders, used to establish the module grid.
  • Alignment patterns: smaller squares present from Version 2 upwards, with more of them in larger versions, which help with perspective distortion.
  • Format and version information: format data is stored twice for redundancy. Version information appears in larger codes (Version 7 and above).

Printing and Scanning Guidelines

  • Contrast and polarity: dark modules on a light background are universally supported. Light-on-dark (inverted) codes and low-contrast color pairs fail on some scanners, so test them or avoid them. A code that looks fine on screen can lose contrast on glossy or textured paper. The contrast-ratio check from our accessible color palettes guide is a quick way to catch weak color pairs, but it was designed for text on screens, so still test an actual scan.
  • Module size over total size: what matters is how big each module is when printed, not just the overall dimensions. A dense Version 10 code needs more space than a Version 2 code to scan from the same distance. Test at the real scanning distance rather than relying on a rule of thumb.
  • Logos: a logo covers modules, spending error correction budget. Use Level Q or H, keep the logo small and centered, never cover the finder patterns, and test the printed result.
  • Surface: avoid placing codes across folds, on curved bottles with strong distortion, or behind reflective glass.
  • Test matrix: scan with at least one iOS and one Android device, the built-in camera app and a third-party scanner, in the lighting where the code will really be used.

Static vs. Dynamic: Trade-offs

A dynamic code encodes a short redirect URL, which keeps the grid small and lets you change the destination later. That convenience comes with dependencies. The redirect service has to stay online for as long as the code is in circulation, so a printed code can die with a vendor or an expired subscription. Use a domain you control. A temporary redirect (302 or 307) is the usual choice so browsers do not cache the old target. See HTTP Redirects: 301 vs 302 and Debugging Chains for the difference.

For campaign tracking, UTM parameters make URLs long, and long URLs mean denser codes. Either keep the parameters few and short, or put them on a short link and let the redirect add them. Read The Art of URL Building: Best Practices for how to keep parameters tidy, and encode values properly.

Security: Scanning Is Clicking

A QR code is opaque to humans, so it is a handy vehicle for phishing, sometimes called quishing. Attackers can stick a fake code over a legitimate one on a poster, parking sign or table. Practical defenses:

  • For people scanning: look at the URL preview before opening, be careful with codes that ask for a login or payment, and check that a printed code has not been stuck over another.
  • For people publishing: use HTTPS, put the code somewhere tamper-evident where practical, and add a short human-readable URL near it so people can verify the domain.
  • For developers building scanners: treat the decoded text as untrusted input. Parse it as a URL, allow only http: and https:, and never inject it into the DOM as HTML or navigate to a javascript: value. The URL validation guide covers this in detail, and the URL parser lets you inspect a decoded link.

Structured Payload Pitfalls

The Wi-Fi format shown below is a de facto convention rather than part of the QR standard itself. Special characters in the network name or password, such as ;, :, ,, " and \, need to be escaped with a backslash, otherwise the scanner will split the fields incorrectly. Set T:nopass for open networks. Likewise, UPI and other payment payloads are structured URIs with strict parameter names, so build them with proper encoding rather than string concatenation. Remember that Wi-Fi and contact codes are plain data, so anyone who sees the code can read the password.

QR Code Use Cases

Payment QR Codes

UPI, Google Pay, and other payment systems use QR codes to encode payment information. These codes contain structured data like merchant ID, amount, and transaction notes.

Wi-Fi Network Sharing

Many phone cameras recognize a Wi-Fi payload such as WIFI:T:WPA;S:NetworkName;P:Password;;. This is a de facto convention, not part of the QR standard (see the escaping notes above).

Contact Information (vCard)

QR codes can store contact information in vCard format, allowing instant contact sharing.

URLs and Deep Links

The most common use: a URL that opens a website or, with the right link type, a mobile app.

Best Practices for QR Code Generation

  • Keep the payload short: fewer characters mean a lower version and larger modules.
  • Choose error correction deliberately: M for clean digital use, Q or H for logos or printed codes that may be damaged, accepting a denser grid.
  • Keep the quiet zone: at least 4 modules of blank margin on every side.
  • Test before printing: scan the final print with several devices and apps at the real viewing distance.

Create Your Own

The QR Code Generator offers custom colors and gradients, adjustable size, all four error correction levels (L, M, Q, H, with M as the default), a UPI payment mode, and PNG download.

One privacy note: our QR generator creates the code in your browser, so the text you enter is not sent anywhere. That is worth checking for any online generator you use, because some build the image on a server and receive your text in the request. Be especially careful with secrets such as Wi-Fi passwords or payment details. Also remember that gradients and low-contrast colors reduce scan reliability, so test them as described above.

Frequently Asked Questions

How much data can a QR code hold?

The largest version (Version 40, 177x177 modules) holds up to 7,089 numeric or 4,296 alphanumeric characters at the lowest error correction level. Real-world codes should stay far below that, because denser codes are harder to scan.

Which error correction level should I choose?

Level M suits most digital and clean printed uses. Choose Q or H if the code carries a logo or may be scuffed outdoors, accepting a denser grid in exchange for resilience.

Why won't my QR code scan?

The usual causes are a missing quiet zone, low contrast or inverted colors, modules printed too small for the distance, a logo that is too large, or too much data. Reduce the payload, enlarge the code and test the actual print.

Do QR codes expire?

A static code never expires, because it contains the data itself. A dynamic code stops working if the redirect service or its domain goes away, so check who controls the redirect before printing.

Are QR codes safe to scan?

Scanning only reads text, but the text can be a malicious link. Preview the URL before opening it, and be wary of codes placed over other codes or those that ask for credentials or payments.

Written by

Spotted a mistake? Let me know