LogicGates.org Open the simulatorSimulator

QR code generator

Type text or a link to make a QR code, then take it apart: every module is coloured by what it does, from the finder patterns to the error correction, and each step of the encoding is written out below.

Digits alone use numeric mode; capitals, digits, space and $ % * + - . / : use alphanumeric mode; anything else uses byte mode, as UTF-8.

Error correction
Symbol Version 1, 21 × 21 modules, level Q, mask 0 11 characters in alphanumeric mode: 13 data codewords and 13 error correction codewords.
1234567891011121314151617181920212223242526

Point at a module, or focus the code and use the arrow keys, to see what it is.

Scannable code

Plain black on white with the four-module quiet zone, ready to print or scan.

Highlight

Mask scores

Each mask is tried and scored with the four penalty rules; the lowest total wins. Lower means fewer patterns that could confuse a scanner.

Mask Total Runs 2×2 boxes Finder-like Balance Use
0chosen 1067 177 90 800 0
1 1230 172 138 920 0
2 1266 205 141 920 0
3 1201 177 144 880 0
4 1339 195 144 1000 0
5 1276 191 165 920 0
6 1114 172 102 840 0
7 1278 198 120 960 0

The three real finder patterns add 720 to the finder-like penalty of every mask, against the quiet zone, so only the differences between the masks matter there.

How this code was built, step by step

The code above, from text to modules. Change the text or the settings and these steps follow.

  1. Mode and character count

    Alphanumeric mode, indicator 0010. The count is 11 characters, written in 9 bits at version 1: 000001011.

  2. Data bits

    Characters go in pairs: 45 × the first value + the second, in 11 bits. A last single character takes 6 bits.

    Characters Value Bits
    HE 779 01100001011
    LL 966 01111000110
    O␣ 1116 10001011100
    WO 1464 10110111000
    RL 1236 10011010100
    D 13 001101
  3. The bit stream, filled to 104 bits

    Version 1 at level Q has room for 13 data codewords. After the data comes a terminator of up to four zeros, zeros to the end of the byte, then pad bytes until it is full.

    Mode indicator 4 bits 0010 which encoding follows
    Character count 9 bits 000001011 how many characters
    Data 61 bits 01100001011 01111000110 10001011100 10110111000 10011010100 001101 the characters
    Terminator 4 bits 0000 end of data
    Zeros to a whole byte 2 bits 00 to a byte boundary
    Pad bytes 24 bits 11101100 00010001 11101100 11101100 and 00010001 in turn
  4. Data codewords

    The stream cut into bytes. Pad bytes are marked.

    20 00100000 #1 5B 01011011 #2 0B 00001011 #3 78 01111000 #4 D1 11010001 #5 72 01110010 #6 DC 11011100 #7 4D 01001101 #8 43 01000011 #9 40 01000000 #10 EC 11101100 pad 11 00010001 pad EC 11101100 pad
  5. Error correction

    One block: the 13 data codewords, divided as a polynomial by the Reed–Solomon generator of degree 13, leave a remainder of 13 error correction codewords.

    Block Data codewords Error correction codewords
    1 20 5B 0B 78 D1 72 DC 4D 43 40 EC 11 EC A8 48 16 52 D9 36 9C 00 2E 0F B4 7A 10
  6. Interleaving and placement

    With one block there is nothing to interleave: the data codewords go first, then the error correction. That makes 26 codewords, placed two columns at a time from the bottom right, zigzagging up and down and stepping round the patterns.

    20 1: D1 5B 2: D2 0B 3: D3 78 4: D4 D1 5: D5 72 6: D6 DC 7: D7 4D 8: D8 43 9: D9 40 10: D10 EC 11: D11 11 12: D12 EC 13: D13 A8 14: E1 48 15: E2 16 16: E3 52 17: E4 D9 18: E5 36 19: E6 9C 20: E7 00 21: E8 2E 22: E9 0F 23: E10 B4 24: E11 7A 25: E12 10 26: E13

    D is data and E error correction: codewords 1–13 are data and 14–26 error correction.

The parts of a QR code

A QR code is a square of modules, 21 × 21 at version 1 and 4 more each way per version, up to 177 × 177 at version 40. Some modules are fixed patterns a scanner uses to find and read the code; the rest carry the data and its error correction.

PartWhere and whatWhy it is there
Finder patterns Three 7 × 7 squares in the corners Their 1:1:3:1:1 ratio is easy to spot at any angle; the missing fourth corner gives the orientation.
Separators A light border one module wide round each finder Keeps the finder pattern apart from the data next to it.
Timing patterns Alternating modules along row 6 and column 6, between the finders Let the scanner count the columns and rows, and so work out the version and the module size.
Alignment patterns 5 × 5 squares from version 2 on: one at version 2, 6 at version 7, 46 at version 40 Reference points that let a scanner correct for a curved or tilted code.
Dark module One module beside the lower left finder, always dark Fixed by the standard; it is never part of the data.
Format information 15 bits, twice: round the top left finder, and split between the other two The error correction level and the mask number, protected by a BCH code. Two copies, so one can be damaged.
Version information 18 bits, twice, in 6 × 3 blocks by the top right and lower left finders, from version 7 The version number, BCH protected, since counting the timing modules gets unreliable in large codes.
Data and error correction Everything else, in 8-module codewords The message and the Reed–Solomon codewords that can rebuild damaged parts of it.
Remainder bits 0, 3, 4 or 7 modules left after the last codeword, depending on the version Filler; they are set to 0 before masking.
Quiet zone A light margin 4 modules wide on every side Separates the code from whatever is printed round it. Without it many scanners fail.

Worked example: 01234567 in numeric mode

The standard's own example, at version 1 and level M, which leaves 16 data codewords and 10 for error correction. The generator above starts with HELLO WORLD in alphanumeric mode, so between them they show both ways of packing characters.

  1. Only digits, so the mode indicator is 0001, and 8 digits in 10 bits is 0000001000.
  2. Digits go in threes, each three written as a 10-bit number: 012 12 0000001100345 345 010101100167 67 1000011 67 is left over as a pair, so it takes 7 bits. Three digits in 10 bits is about 3.3 bits a digit, against 8 in byte mode.
  3. That is 41 bits. 4 terminator zeros, 3 more to finish the byte, and 10 pad bytes make the 16 data codewords: 10 20 0C 56 61 80 EC 11 EC 11 EC 11 EC 11 EC 11
  4. Reed–Solomon over GF(256) gives the 10 error correction codewords: A5 24 D4 C1 ED 36 C7 87 2C 55
  5. The 26 codewords fill the 208 free modules exactly; version 1 has no remainder bits. Mask 0 scores lowest here, and the format information for level M and mask 0 is 101010000010010. Level M is 00 and mask 0 is 000, so the five data bits and their BCH bits are all zero, and what is left is the fixed pattern the format bits are XORed with.

Open 01234567 in the generator. The codewords are bytes like any other; the hex to binary converter turns them back into the bits in the modules.

Error correction levels

Reed–Solomon codewords are added to every block. A scanner can rebuild damaged codewords from them, up to the share each level is designed for. The cost is room for data.

Level Restores about Format bits Data codewords, v1 Bytes, v1 Bytes, v10
L 7% 01 19 17 271
M 15% 00 16 14 213
Q 25% 11 13 11 151
H 30% 10 9 7 119

The format bits are not in alphabetical order: the standard numbers the levels M = 00, L = 01, H = 10, Q = 11.

The eight masks

Data can come out in stripes or blotches, or contain something that looks like a finder pattern. So the data and error correction modules are XORed with one of eight fixed patterns, flipping the modules where the pattern is dark, and the pattern that leaves the fewest problems is kept. Row and column count from 0 at the top left.

Mask 0 (row + column) mod 2 = 0
Mask 1 row mod 2 = 0
Mask 2 column mod 3 = 0
Mask 3 (row + column) mod 3 = 0
Mask 4 (⌊row / 2⌋ + ⌊column / 3⌋) mod 2 = 0
Mask 5 (row × column) mod 2 + (row × column) mod 3 = 0
Mask 6 ((row × column) mod 2 + (row × column) mod 3) mod 2 = 0
Mask 7 ((row + column) mod 2 + (row × column) mod 3) mod 2 = 0

The penalty rules, from ISO/IEC 18004:

  • Runs. Five or more modules of one colour in a row or column score 3, plus 1 for each module past five.
  • Boxes. Each 2 × 2 block of one colour scores 3. Blocks can overlap.
  • Finder-like patterns. Dark, light, dark, light, dark in the ratio 1:1:3:1:1 with four light modules before or after it scores 40. A pattern with four light modules on both sides counts twice, 80. This page counts the quiet zone as light, so the three real finder patterns always score 720 between them; only the rest differs from mask to mask.
  • Balance. 10 for every whole 5% the share of dark modules is away from half.

The rules leave room for interpretation, at the edges especially, so two correct generators can choose different masks for the same text. Both codes scan, because the mask number is in the format information.

QR code capacity by version

How much fits in each version: bytes at each level (byte mode, such as a link with lower case letters), and digits and alphanumeric characters at level M.

Version Modules Alignment Bytes L Bytes M Bytes Q Bytes H Digits M Alnum M
1 21×21 0 1714117 34 20
2 25×25 1 32262014 63 38
3 29×29 1 53423224 101 61
4 33×33 1 78624634 149 90
5 37×37 1 106846044 202 122
6 41×41 1 1341067458 255 154
7 45×45 6 1541228664 293 178
8 49×49 6 19215210884 365 221
9 53×53 6 23018013098 432 262
10 57×57 6 271213151119 513 311
11 61×61 6 321251177137 604 366
12 65×65 6 367287203155 691 419
13 69×69 6 425331241177 796 483
14 73×73 13 458362258194 871 528
15 77×77 13 520412292220 991 600
16 81×81 13 586450322250 1,082 656
17 85×85 13 644504364280 1,212 734
18 89×89 13 718560394310 1,346 816
19 93×93 13 792624442338 1,500 909
20 97×97 13 858666482382 1,600 970
21 101×101 22 929711509403 1,708 1,035
22 105×105 22 1,003779565439 1,872 1,134
23 109×109 22 1,091857611461 2,059 1,248
24 113×113 22 1,171911661511 2,188 1,326
25 117×117 22 1,273997715535 2,395 1,451
26 121×121 22 1,3671,059751593 2,544 1,542
27 125×125 22 1,4651,125805625 2,701 1,637
28 129×129 33 1,5281,190868658 2,857 1,732
29 133×133 33 1,6281,264908698 3,035 1,839
30 137×137 33 1,7321,370982742 3,289 1,994
31 141×141 33 1,8401,4521,030790 3,486 2,113
32 145×145 33 1,9521,5381,112842 3,693 2,238
33 149×149 33 2,0681,6281,168898 3,909 2,369
34 153×153 33 2,1881,7221,228958 4,134 2,506
35 157×157 46 2,3031,8091,283983 4,343 2,632
36 161×161 46 2,4311,9111,3511,051 4,588 2,780
37 165×165 46 2,5631,9891,4231,093 4,775 2,894
38 169×169 46 2,6992,0991,4991,139 5,039 3,054
39 173×173 46 2,8092,2131,5791,219 5,313 3,220
40 177×177 46 2,9532,3311,6631,273 5,596 3,391

A character outside ASCII takes 2 to 4 bytes in UTF-8, so text with accents or emoji fits fewer characters than the byte count. The binary translator shows the UTF-8 bytes of any text, and the ASCII table the one-byte characters. This generator writes UTF-8 with no ECI header; without one the standard's default for byte mode is ISO-8859-1, but most scanners recognise UTF-8 by its byte patterns.

Common mistakes

  • No quiet zone. Printing the code flush against a border, a photo or other text. Leave four modules of light margin on every side.
  • Too dense to scan. A long link makes a high version with small modules. Shorten the link, or print the code bigger.
  • Lower case in a link that could be capitals. One lower case letter forces byte mode. At level Q, https://logicgates.org needs version 3; HTTPS://LOGICGATES.ORG fits version 2. Scheme and domain are not case sensitive, but the path after them can be.
  • Low contrast or inverted colours. Dark modules on a light background is the normal form and the safest. Many phone scanners also read light on dark, but not all.
  • A logo over too much of it. A logo destroys the modules under it, which error correction has to make up for. Use level H, keep the logo small and away from the finder patterns, and test the result.

Questions

What are the three big squares in a QR code?

Finder patterns. Each is a 7 × 7 square of a dark ring, a light ring and a dark 3 × 3 centre, so a line through it in any direction crosses dark, light, dark, light and dark in the ratio 1:1:3:1:1. A scanner looks for that ratio to find the code and works out its angle from where the three squares sit; the corner without one is the bottom right.

Which error correction level should I use?

Level M, which can restore about 15% of the codewords, suits most printed codes. Use L (about 7%) to keep a long link small on a clean screen, and Q (25%) or H (30%) for codes that get scratched or have a logo over them. More correction means a bigger symbol: https://logicgates.org/qr-code-generator needs version 3 at L and version 5 at H.

Why does lower case text make a bigger QR code?

Alphanumeric mode, at 5.5 bits a character, only has the capitals, the digits, space and $ % * + - . / :. One lower case letter sends the whole text to byte mode, at 8 bits a character. https://logicgates.org is 176 bits of data in byte mode; HTTPS://LOGICGATES.ORG is 121 bits in alphanumeric mode, and domain names are not case sensitive.

Why does another generator make a different pattern for the same text?

The text can be encoded in more than one valid way. Generators differ in which version and error correction level they pick, whether they mix modes, and how they score the eight masks, so the modules differ while the content is the same. Any scanner reads them all, because the format information in the code says which level and mask were used.

Do QR codes expire?

No. A QR code is just the text or link drawn as modules, so it works for as long as the print survives and, for a link, the page exists. Codes sold as "dynamic" hold a link to a redirect service, and those stop working if that service is switched off. The codes made here are static: nothing is stored or tracked.

What is the most a QR code can hold?

Version 40 at level L, 177 × 177 modules, holds 7,089 digits, 4,296 alphanumeric characters or 2,953 bytes. Codes that big are hard to scan from a phone, so links are usually kept short.

Bars instead of squares: the EAN-13 barcode generator draws the barcodes on shop products, with each digit's bars explained. For another way to write bytes as text, see Base64.