Skip to main content

Structure Final Message

In the error correction coding section, the Reed-Solomon error correction process was explained. You should now have the data codewords and their corresponding error correction codewords. As mentioned in the previous step, larger QR codes require you to break the data codewords into smaller blocks, and generate error correction codewords separately for each block. When this is the case, the data blocks and error correction codewords must be interleaved according to the QR code specification. This page explains the interleaving process in detail.

Step 1: Determine How Many Blocks and Error Correction Codewords are Required

The error correction table shows how many data blocks and error correction codewords per block are required for each version and error correction level.

For Smaller Codes, Use Data and Error Correction Codewords As-Is

Please note that smaller QR codes only consist of one block of data codewords, with one set of error correction codewords for that block. In this case, no interleaving is necessary. Simply place the error correction codewords after the data codewords and move on to the next step, module placement in matrix.

Break Up Larger QR Codes

On the error correction coding page, I showed an example using a 5-Q code. The error correction table says that a 5-Q code's first group consists of 2 blocks, with 15 data codewords per block, and the second group consists of 2 blocks, with 16 data codewords per block. Here again is the table shown on the error correction coding page that showed the 5-Q code broken up into groups and blocks.

Group NumberBlock NumberData Codewords in the Group
Group 1Block 1 (codeword #1) 01000011
(codeword #2) 01010101
(codeword #3) 01000110
(codeword #4) 10000110
(codeword #5) 01010111
(codeword #6) 00100110
(codeword #7) 01010101
(codeword #8) 11000010
(codeword #9) 01110111
(codeword #10) 00110010
(codeword #11) 00000110
(codeword #12) 00010010
(codeword #13) 00000110
(codeword #14) 01100111
(codeword #15) 00100110
Block 2(codeword #16) 11110110
(codeword #17) 11110110
(codeword #18) 01000010
(codeword #19) 00000111
(codeword #20) 01110110
(codeword #21) 10000110
(codeword #22) 11110010
(codeword #23) 00000111
(codeword #24) 00100110
(codeword #25) 01010110
(codeword #26) 00010110
(codeword #27) 11000110
(codeword #28) 11000111
(codeword #29) 10010010
(codeword #30) 00000110
Group 2Block 1(codeword #31) 10110110
(codeword #32) 11100110
(codeword #33) 11110111
(codeword #34) 01110111
(codeword #35) 00110010
(codeword #36) 00000111
(codeword #37) 01110110
(codeword #38) 10000110
(codeword #39) 01010111
(codeword #40) 00100110
(codeword #41) 01010010
(codeword #42) 00000110
(codeword #43) 10000110
(codeword #44) 10010111
(codeword #45) 00110010
(codeword #46) 00000111
Block 2(codeword #47) 01000110
(codeword #48) 11110111
(codeword #49) 01110110
(codeword #50) 01010110
(codeword #51) 11000010
(codeword #52) 00000110
(codeword #53) 10010111
(codeword #54) 00110010
(codeword #55) 11100000
(codeword #56) 11101100
(codeword #57) 00010001
(codeword #58) 11101100
(codeword #59) 00010001
(codeword #60) 11101100
(codeword #61) 00010001
(codeword #62) 11101100

Notice that the error correction table says that for each block, a 5-Q code must have 18 error correction codewords. Now that error correction coding has been explained, the error correction codewords for the above data can be calculated.

There are four blocks, so four sets of 18 error correction codewords should be generated. The following is an updated table that shows the data correction codewords of each block converted into a list of integers, and the 18 error correction codewords generated for each block.

Group NumberBlock NumberData Codewords in the GroupData Codewords as Integers
Error Correction Codewords for the Block
Group 1Block 1 (codeword #1) 01000011
(codeword #2) 01010101
(codeword #3) 01000110
(codeword #4) 10000110
(codeword #5) 01010111
(codeword #6) 00100110
(codeword #7) 01010101
(codeword #8) 11000010
(codeword #9) 01110111
(codeword #10) 00110010
(codeword #11) 00000110
(codeword #12) 00010010
(codeword #13) 00000110
(codeword #14) 01100111
(codeword #15) 00100110
Data Codewords from the left, as Integers: 67,85,70,134,87,38,85,194,119,50,6,18,6,103,38

Error Correction Codewords (show how these were calculated):
213 199 11 45 115 247 241 223 229 248 154 117 154 111 86 161 111 39
Block 2(codeword #16) 11110110
(codeword #17) 11110110
(codeword #18) 01000010
(codeword #19) 00000111
(codeword #20) 01110110
(codeword #21) 10000110
(codeword #22) 11110010
(codeword #23) 00000111
(codeword #24) 00100110
(codeword #25) 01010110
(codeword #26) 00010110
(codeword #27) 11000110
(codeword #28) 11000111
(codeword #29) 10010010
(codeword #30) 00000110
Data Codewords from the left, as Integers:
246,246,66,7,118,134,242,7,38,86,22,198,199,146,6

Error Correction Codewords (show how these were calculated):
87 204 96 60 202 182 124 157 200 134 27 129 209 17 163 163 120 133
Group 2Block 1(codeword #31) 10110110
(codeword #32) 11100110
(codeword #33) 11110111
(codeword #34) 01110111
(codeword #35) 00110010
(codeword #36) 00000111
(codeword #37) 01110110
(codeword #38) 10000110
(codeword #39) 01010111
(codeword #40) 00100110
(codeword #41) 01010010
(codeword #42) 00000110
(codeword #43) 10000110
(codeword #44) 10010111
(codeword #45) 00110010
(codeword #46) 00000111
Data Codewords from the left, as Integers:
182,230,247,119,50,7,118,134,87,38,82,6,134,151,50,7

Error Correction Codewords (show how these were calculated):
148 116 177 212 76 133 75 242 238 76 195 230 189 10 108 240 192 141
Block 2(codeword #47) 01000110
(codeword #48) 11110111
(codeword #49) 01110110
(codeword #50) 01010110
(codeword #51) 11000010
(codeword #52) 00000110
(codeword #53) 10010111
(codeword #54) 00110010
(codeword #55) 11100000
(codeword #56) 11101100
(codeword #57) 00010001
(codeword #58) 11101100
(codeword #59) 00010001
(codeword #60) 11101100
(codeword #61) 00010001
(codeword #62) 11101100
Data Codewords from the left, as Integers:
70,247,118,86,194,6,151,50,16,236,17,236,17,236,17,236

Error Correction Codewords (show how these were calculated):
235 159 5 173 24 147 59 33 106 40 255 172 82 2 131 32 178 236

Now that the error correction codewords for the data have been calculated, the next section explains the process of interleaving the blocks of data and error correction codewords.

Step 2: Interleave the Blocks

The blocks are interleaved by doing the following:

  1. take the first data codeword from the first block
  2. followed by the first data codeword from the second block
  3. followed by the first data codeword from the third block
  4. followed by the first data codeword from the fourth block
  5. followed by the second data codeword from the first block
  6. and so on

This pattern is repeated, going across the blocks, until all of the data codewords have been interleaved.

After that, do the following:

  1. take the first error correction codeword from the first block
  2. followed by the first error correction codeword from the second block
  3. followed by the first error correction codeword from the third block
  4. followed by the first error correction codeword from the fourth block
  5. followed by the second error correction codeword from the first block
  6. and so on

Do this until all error correction codewords have been used up.

This process is detailed below.

Interleave the Data Codewords

The process of interleaving the data codewords is illustrated with the table and description below.

Col 1Col 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16
Block 167857013487388519411950618610338
Block 224624666711813424273886221981991466
Block 31822302471195071181348738826134151507
Block 4702471188619461515016236172361723617236

As described above, take the first data codeword from the first block, followed by the first data codeword from the second block, followed by the first data codeword from the third block, followed by the first data codeword from the fourth block.

Or, as shown in the table above, take the data codewords from column 1, starting with block 1 and ending with block 4. The numbers from column 1 are 67, 246, 182, and 70.

Interleaved data so far:
67, 246, 182, 70

After that, put the numbers from column 2, starting with block 1 and ending with block 4.

Interleaved data so far:
67, 246, 182, 70, 85, 246, 230, 247

Do the same with column 3.

Interleaved data so far:
67, 246, 182, 70, 85, 246, 230, 247, 70, 66, 247, 118

Repeat the process, going column by column. Here is the result after adding column 15.

Interleaved data after adding column 15:
67, 246, 182, 70, 85, 246, 230, 247, 70, 66, 247, 118, 134, 7, 119, 86, 87, 118, 50, 194, 38, 134, 7, 6, 85, 242, 118, 151, 194, 7, 134, 50, 119, 38, 87, 16, 50, 86, 38, 236, 6, 22, 82, 17, 18, 198, 6, 236, 6, 199, 134, 17, 103, 146, 151, 236, 38, 6, 50, 17

Column 16 only has two numbers in it, as you can see in the table above. Simply put these two numbers at the end of the interleaved data like so:

Interleaved data codewords:
67, 246, 182, 70, 85, 246, 230, 247, 70, 66, 247, 118, 134, 7, 119, 86, 87, 118, 50, 194, 38, 134, 7, 6, 85, 242, 118, 151, 194, 7, 134, 50, 119, 38, 87, 16, 50, 86, 38, 236, 6, 22, 82, 17, 18, 198, 6, 236, 6, 199, 134, 17, 103, 146, 151, 236, 38, 6, 50, 17, 7, 236

Interleave the Error Correction Codewords

Now that the data codewords have been interleaved, the error correction codewords should be interleaved. This process is illustrated with the table and description below.

Col 1Col 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Col 12Col 13Col 14Col 15Col 16Col 17Col 18
Block 121319911451152472412232292481541171541118616111139
Block 28720496602021821241572001342712920917163163120133
Block 314811617721276133752422387619523018910108240192141
Block 423515951732414759331064025517282213132178236

As described above, take the first error correction codeword from the first block, followed by the first error correction codeword from the second block, followed by the first error correction codeword from the third block, followed by the first error correction codeword from the fourth block. Or, as shown in the table above, take the error correction codewords from column 1, starting with block 1 and ending with block 4. The numbers from column 1 are 213, 87, 148, and 235.

Interleaved error correction codewords so far:
213, 87, 148, 235

After that, put the numbers from column 2, starting with block 1 and ending with block 4.

Interleaved error correction codewords so far:
213, 87, 148, 235, 199, 204, 116, 159

Do the same with column 3.

Interleaved error correction codewords so far:
213, 87, 148, 235, 199, 204, 116, 159, 11, 96, 177, 5

Repeat the process, going column by column. Here is the final result.

Interleaved error correction codewords:
213, 87, 148, 235, 199, 204, 116, 159, 11, 96, 177, 5, 45, 60, 212, 173, 115, 202, 76, 24, 247, 182, 133, 147, 241, 124, 75, 59, 223, 157, 242, 33, 229, 200, 238, 106, 248, 134, 76, 40, 154, 27, 195, 255, 117, 129, 230, 172, 154, 209, 189, 82, 111, 17, 10, 2, 86, 163, 108, 131, 161, 163, 240, 32, 111, 120, 192, 178, 39, 133, 141, 236

Put Interleaved Error Correction Codewords After Interleaved Data Codewords

The final message consists of the interleaved data codewords followed by the interleaved error correction codewords.

Final message:
67, 246, 182, 70, 85, 246, 230, 247, 70, 66, 247, 118, 134, 7, 119, 86, 87, 118, 50, 194, 38, 134, 7, 6, 85, 242, 118, 151, 194, 7, 134, 50, 119, 38, 87, 16, 50, 86, 38, 236, 6, 22, 82, 17, 18, 198, 6, 236, 6, 199, 134, 17, 103, 146, 151, 236, 38, 6, 50, 17, 7, 236, 213, 87, 148, 235, 199, 204, 116, 159, 11, 96, 177, 5, 45, 60, 212, 173, 115, 202, 76, 24, 247, 182, 133, 147, 241, 124, 75, 59, 223, 157, 242, 33, 229, 200, 238, 106, 248, 134, 76, 40, 154, 27, 195, 255, 117, 129, 230, 172, 154, 209, 189, 82, 111, 17, 10, 2, 86, 163, 108, 131, 161, 163, 240, 32, 111, 120, 192, 178, 39, 133, 141, 236

Step 3: Convert to Binary

The final message is converted to 8-bit binary. In our example, the first four codewords are 67, 246, 182, and 70. Converted to binary, these are:

67 = 01000011
246 = 11110110
182 = 10110110
70 = 01000110

The 8-bit binary numbers are put together in an unbroken string. Using the first four codewords from above, the first 32 bits of the final message are:

01000011111101101011011001000110

For completeness, the entire final message in binary is in the text box below:

Step 4: Add Remainder Bits if Necessary

For some QR versions, the final binary message is not long enough to fill the required number of bits. In this case, it is necessary to add a certain number of 0s to the end of the final message to make it have the correct length. These extra 0s are called remainder bits. A version 5 QR code, like the one in this example, must have 7 remainder bits added to the end.

The final 8 bits from the string in the text box above are:
11101100

With the 7 remainder bits added to the end, the final bits of the string are:
111011000000000

For completeness, the entire final message in binary, with the 7 remainder bits added, is in the text box below:

See below for a table that lists the required number of remainder bits for each QR version.

Now that the final binary string has been determined, the next step is to place the data bits and function patterns in the QR code matrix. This process is explained in the module placement in matrix section.

List of Versions and Required Remainder Bits

The following table lists the 40 QR versions and the number of remainder bits that must be added to the end of the final message string. Note that the remainder bits specified for each version are required no matter which error correction level is in use. Also note that some versions, such as versions 7 through 13, do not require any remainder bits to be added.

QR VersionRequired Remainder Bits
10
27
37
47
57
67
70
80
90
100
110
120
130
143
153
163
173
183
193
203
214
224
234
244
254
264
274
283
293
303
313
323
333
343
350
360
370
380
390
400

Next Step

As mentioned above, the next step is the module placement in matrix. This section explains how to place the data bits as well as the function patterns that all QR codes require.