Spec & internals
How a QR code is generated, step by step
Encoding runs in eight steps: choose the mode, choose the version, build the bit stream, split into codewords, compute Reed–Solomon error correction, interleave, place modules in a zig-zag, then try all eight masks and keep the one with the lowest penalty score.
1. Choose the encoding mode
The encoder segments the input and picks the cheapest mode for each run: numeric at 3.33 bits per character, alphanumeric at 5.5, byte at 8, kanji at 13. Switching costs a mode indicator and a character count, so very short runs are not worth splitting.
2. Choose the version
The smallest version whose data capacity at the chosen error-correction level holds the bit stream. Version 1 is 21 × 21 modules; version 40 is 177 × 177.
3. Build the bit stream
For each segment: a 4-bit mode indicator, a character count whose width depends on mode and
version, then the encoded data. Then a 4-bit terminator, padding to a byte boundary, and the
alternating pad bytes 11101100 00010001 until the version's data capacity is full.
4. Split into codewords and blocks
The stream is cut into 8-bit codewords and divided into blocks. The block structure — how many blocks, and how many codewords in each — is fixed per version and level by a table in the standard.
5. Compute Reed–Solomon error correction
Each block gets its own error-correction codewords, computed over GF(256). The count per block is what determines the 7% / 15% / 25% / 30% damage tolerance.
6. Interleave
Codewords from different blocks are woven together, so a contiguous physical scratch spreads its damage across blocks rather than destroying one. This is why a thin scratch across a whole code is often survivable.
7. Place the modules
Function patterns first — finder patterns, alignment patterns, timing patterns, the dark module, and reserved areas for format and version information.
Then data is placed in a zig-zag: two-module-wide columns filled upward, then downward, starting from the bottom right, skipping every reserved region.
8. Mask and score
All eight mask patterns are applied in turn to the data area, and each result is scored on four penalty rules: runs of five or more same-coloured modules, 2 × 2 same-coloured blocks, finder-like 1:1:3:1:1 runs, and overall dark/light imbalance. The lowest total wins.
Finally the format information — level and mask, with its own BCH error correction — is written into its reserved cells, twice.
Why two generators produce different-looking codes
Mask selection. If two implementations differ slightly in how they apply the penalty rules, they choose different masks for the same data, producing visually different but equally valid codes. Both decode to the same string.
Then verify it
Rendering the matrix is not the end. Styling, colour and logos all consume error-correction budget invisibly, so the only way to know the output scans is to rasterise it and read it back with a real decoder — see why you should verify.
FAQ
How is a QR code created?
By choosing an encoding mode and version, building a padded bit stream, computing Reed–Solomon error correction, interleaving the codewords, placing them in a zig-zag around the function patterns, and selecting the best of eight mask patterns.
Why do two QR codes for the same text look different?
Mask selection. Implementations that score the eight mask patterns slightly differently choose different masks, producing different-looking but equally valid codes.
What is the zig-zag placement?
Data modules are written in two-module-wide columns running alternately upward and downward from the bottom right, skipping the reserved function-pattern regions.
What are the pad bytes in a QR code?
After the terminator, the stream is filled to capacity with the alternating bytes 11101100 and 00010001, which is why short payloads still fill their version completely.