How To Decode QR Code Algorithm

How To Decode QR Code Algorithm is a surprisingly structured process — once you understand the layers of finder patterns, format strips, mask XOR, and Reed-Solomon error correction, the black-and-white grid becomes completely readable by hand or code.

By Neo

What Is a QR Code?

A QR (Quick Response) code is a two-dimensional matrix barcode invented by Denso Wave in 1994. Unlike a traditional 1D barcode that encodes data in one axis of parallel lines, a QR code stores data in a grid of black and white square modules arranged in a square pattern. The physical size of that grid is called the version— Version 1 is 21×21 modules, and each additional version adds 4 modules per side, up to Version 40 at 177×177 modules.

Anatomy of a QR Code

Every QR code shares a fixed structure regardless of its version:

  • Finder Patterns: Three identical 7×7 squares located in the top-left, top-right, and bottom-left corners. Their distinctive pattern of concentric black/white/black rings allows scanners to detect the code from any angle and calculate its orientation.
  • Separator: A one-module-wide white border around each finder pattern to visually isolate it from the data area.
  • Timing Patterns: Alternating black/white modules running horizontally and vertically between the finder patterns, starting at row/column 6. They establish the module grid coordinate system.
  • Alignment Patterns: Smaller 5×5 positioning squares placed throughout the grid in Version 2 and above to compensate for perspective distortion when reading from an angle.
  • Format Information Strip: Two copies of a 15-bit sequence placed around the top-left finder pattern encoding the error correction level and mask pattern number. This is the first thing a decoder reads.
  • Data and Error Correction Region: The remaining modules hold the actual encoded payload interleaved with Reed-Solomon error correction codewords.

QR Code Data Encoding Modes

Before data bits are placed in the grid, the encoder chooses an encoding mode that determines how characters are packed into bits. Each mode begins with a 4-bit mode indicator so the decoder knows how to interpret what follows. Choosing the most compact mode for the payload produces a smaller, denser QR code.

ModeMode Indicator (4 bits)Character SetBits per Character
Numeric0001Digits 0–9 only~3.33 (groups of 3 in 10 bits)
Alphanumeric0010A–Z, 0–9, space, $%*+-./: ~5.5 (pairs in 11 bits)
Byte0100Full ASCII / UTF-8 (ISO 8859-1)8 bits per character
Kanji1000Shift JIS double-byte Japanese characters13 bits per character

The mode indicator is followed by a character count indicator whose bit-length depends on both the mode and the QR version, and then the actual encoded characters. Byte mode is the most versatile — it passes raw octets straight through, which is why Byte mode uses ASCII/UTF-8 character codes that map directly to standard ASCII values.

How the QR Code Algorithm Decodes Step by Step

A QR code decoder — whether running in a phone camera, a web app, or your own code — follows these six deterministic steps in sequence:

Step 1: Locate Finder Patterns to Orient the Image

The decoder scans the image for the three distinctive finder patterns — concentric 7×7, 5×5, and 3×3 squares — in three corners. The ratio of black to white modules crossing any finder pattern is always 1:1:3:1:1, making them detectable from any rotation or partial perspective. Once found, their relative positions define which corner is top-left, establish the module grid size, and correct for any rotation or tilt in the captured image.

Step 2: Read Format Information (Error Correction Level + Mask Pattern)

The format information strip sits immediately adjacent to the top-left finder pattern (with a duplicate copy near the other two finders as backup). It encodes 15 bits: 2 bits for the error correction level and 3 bits for the mask pattern number, all protected by 10 BCH error-correcting bits. The decoder reads this strip first because every subsequent step depends on knowing the mask pattern.

Step 3: Unmask the Data Modules Using XOR

QR encoders apply one of eight mask patterns to the data region to prevent large uniform areas of black or white that would confuse scanners. The mask is a bitwise XOR: dark modules stay dark where the mask is 0, and flip where it is 1. Decoding simply re-applies the same XOR operation to reverse it — a given module's decoded bit equals its raw value XOR the mask bit at that position.

Step 4: Read Data Bits in a Zigzag Pattern from Bottom-Right

Data modules are read in 2-column-wide vertical strips starting at the bottom-right corner of the grid and snaking upward. Within each 2-module-wide strip the bits alternate right-column/left-column as they travel up or down. Functional modules (finder patterns, timing, alignment, format info) are skipped automatically — the zigzag path treats them as if they do not exist. This produces the complete ordered stream of data and error correction bits.

Step 5: Apply Reed-Solomon Error Correction

The raw bit stream is split into 8-bit codewords, which are further divided into data codewords and error correction codewords. Reed-Solomon decoding checks the error correction codewords against the data codewords and repairs any differences. Depending on the error correction level, up to 7–30% of codewords can be incorrect and the decoder will still recover the original data perfectly.

Step 6: Extract Mode Indicator, Character Count, and Data

After error correction, the decoder reads the first 4 bits as the mode indicator, then the character count indicator, then the character data itself — all according to the rules of the detected mode (Numeric, Alphanumeric, Byte, or Kanji). A terminator sequence of four zero bits signals the end of data. The resulting bytes are then decoded into the final string.

Reed-Solomon Error Correction

Reed-Solomon is a polynomial-based forward error correction scheme operating over Galois fields. For QR codes it works at the byte (codeword) level: the encoder computes a set of check codewords from the data codewords using polynomial division, and appends them. The decoder performs the reverse: it evaluates whether the received codewords satisfy the polynomial equations and, if not, locates and corrects the errors. No retransmission is needed — the correction is entirely local.

QR codes define four error correction levels that trade payload capacity for resilience. A higher level means more of the symbol area is dedicated to check codewords, leaving less room for data:

LevelNameData Recovery CapacityTypical Use
LLow~7%Clean, undamaged environments; maximum data density
MMedium~15%General purpose; common default
QQuartile~25%Industrial printing where smudging is common
HHigh~30%Logo overlays; harsh physical environments

This is why a QR code with a brand logo printed over its center still scans correctly — the logo destroys some modules, but Reed-Solomon at level H has enough redundancy to reconstruct the missing data without guessing.

Practical Online QR Decoding

For everyday use, most modern smartphone cameras decode QR codes automatically and instantly — you simply point and tap the notification. The entire six-step algorithm runs in milliseconds inside the camera app's image processing pipeline.

For developers or puzzle solvers who want to inspect the raw decoded output — the actual mode, character count, and byte values — dedicated QR reader tools are more useful than a phone camera. Look for readers that expose the raw data string, the encoding mode used, and optionally the error correction level. This lets you verify, for example, whether the QR code used Byte mode and whether the payload is plain text, a URL, or further encoded data.

If the decoded payload still looks scrambled after reading, the content itself may be encoded in a secondary scheme. Use our Cipher Identifier to figure out what encoding or cipher was applied to the QR content. For data hidden inside image files rather than QR codes, our steganography image decoder can extract concealed payloads embedded in image pixel data.

What QR Codes Actually Encode

Despite their visual complexity, QR codes simply store a string of characters. The meaning of that string is determined entirely by convention and the application reading it. Common payloads include:

  • URLs: By far the most common use case. The string starts with https:// and browsers open it automatically.
  • Plain text: Any freeform text string, often used for short messages, coupon codes, or serial numbers.
  • vCard contact data: Structured text beginning with BEGIN:VCARDencoding a contact's name, phone, email, and address.
  • WiFi credentials: A standardized format beginning with WIFI: followed by SSID, security type, and password, allowing one-scan network joining.
  • Base64-encoded data: QR codes in Byte mode often encode Base64 data — useful for embedding binary payloads like images or cryptographic keys in a text-safe format that fits within the QR symbol.

Understanding the payload format is separate from understanding the QR decoding algorithm. The algorithm only recovers the raw string — interpreting that string is the next layer of the puzzle.

FAQ

What is the QR code decoding algorithm?

The QR code decoding algorithm is a six-step deterministic process: locate the three finder patterns to orient the image, read the format information strip to get the error correction level and mask pattern, XOR-unmask the data modules, read bits in a zigzag from bottom-right, apply Reed-Solomon error correction, then extract the mode indicator, character count, and encoded data string.

What are the four QR code encoding modes?

QR codes support Numeric (mode indicator 0001) for digits 0–9, Alphanumeric (0010) for A–Z, digits, and a few symbols, Byte (0100) for full ASCII/UTF-8 data, and Kanji (1000) for Japanese double-byte characters. Each mode optimizes bit usage for its character set.

What is Reed-Solomon error correction in QR codes?

Reed-Solomon is a forward error-correcting code that lets QR codes recover from physical damage. QR codes have four error correction levels — L (~7%), M (~15%), Q (~25%), and H (~30%) — indicating how much of the codeword data can be reconstructed even if that portion of the symbol is destroyed.

How does the QR code mask pattern work?

After data bits are placed in the QR grid, one of eight mask patterns is XOR-applied to the data modules to prevent large uniform areas that confuse scanners. The mask pattern number (0–7) is stored in the format information strip so the decoder can re-apply the same XOR and reverse it during decoding.

Can a damaged QR code still be decoded?

Yes. At error correction level H, up to 30% of the QR code's modules can be damaged or obscured and the symbol still decodes correctly. This is why brand logos are commonly placed over the center of QR codes — the Reed-Solomon redundancy absorbs the missing data without any loss of the final payload.