/// <summary> /// Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24). /// </summary> /// <param name="numDataBytes">The num data bytes.</param> /// <param name="bits">The bits.</param> internal static void TerminateBits(int numDataBytes, BitArray bits) { int capacity = numDataBytes << 3; if (bits.Size > capacity) { throw new WriterException("data bits cannot fit in the QR Code" + bits.Size + " > " + capacity); } for (int i = 0; i < 4 && bits.Size < capacity; ++i) { bits.AppendBit(false); } // Append termination bits. See 8.4.8 of JISX0510:2004 (p.24) for details. // If the last byte isn't 8-bit aligned, we'll add padding bits. int numBitsInLastByte = bits.Size & 0x07; if (numBitsInLastByte > 0) { for (int i = numBitsInLastByte; i < 8; i++) { bits.AppendBit(false); } } // If we have more space, we'll fill the space with padding patterns defined in 8.4.9 (p.24). int numPaddingBytes = numDataBytes - bits.SizeInBytes; for (int i = 0; i < numPaddingBytes; ++i) { bits.AppendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8); } if (bits.Size != capacity) { throw new WriterException("Bits size does not equal capacity"); } }