appendBit() public method

Appends the bit.
public appendBit ( bool bit ) : void
bit bool The bit.
return void
Ejemplo n.º 1
0
 public void testAppendBit()
 {
    BitArray v = new BitArray();
    Assert.AreEqual(0, v.SizeInBytes);
    // 1
    v.appendBit(true);
    Assert.AreEqual(1, v.Size);
    Assert.AreEqual(0x80000000L, getUnsignedInt(v, 0));
    // 10
    v.appendBit(false);
    Assert.AreEqual(2, v.Size);
    Assert.AreEqual(0x80000000L, getUnsignedInt(v, 0));
    // 101
    v.appendBit(true);
    Assert.AreEqual(3, v.Size);
    Assert.AreEqual(0xa0000000L, getUnsignedInt(v, 0));
    // 1010
    v.appendBit(false);
    Assert.AreEqual(4, v.Size);
    Assert.AreEqual(0xa0000000L, getUnsignedInt(v, 0));
    // 10101
    v.appendBit(true);
    Assert.AreEqual(5, v.Size);
    Assert.AreEqual(0xa8000000L, getUnsignedInt(v, 0));
    // 101010
    v.appendBit(false);
    Assert.AreEqual(6, v.Size);
    Assert.AreEqual(0xa8000000L, getUnsignedInt(v, 0));
    // 1010101
    v.appendBit(true);
    Assert.AreEqual(7, v.Size);
    Assert.AreEqual(0xaa000000L, getUnsignedInt(v, 0));
    // 10101010
    v.appendBit(false);
    Assert.AreEqual(8, v.Size);
    Assert.AreEqual(0xaa000000L, getUnsignedInt(v, 0));
    // 10101010 1
    v.appendBit(true);
    Assert.AreEqual(9, v.Size);
    Assert.AreEqual(0xaa800000L, getUnsignedInt(v, 0));
    // 10101010 10
    v.appendBit(false);
    Assert.AreEqual(10, v.Size);
    Assert.AreEqual(0xaa800000L, getUnsignedInt(v, 0));
 }
Ejemplo n.º 2
0
 /// <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");
    }
 }
Ejemplo n.º 3
0
 private static BitArray toBitArray(string bits)
 {
    var @in = new BitArray();
    var str = DOTX.Replace(bits, "");
    foreach (char aStr in str)
    {
       @in.appendBit(aStr == 'X');
    }
    return @in;
 }
Ejemplo n.º 4
0
        public static byte[] Encode(byte[] Content, int Width, int Height)
        {
            var ecLevel = ErrorCorrectionLevel.L;
            var bits = new BitArray();

            var mode = Mode.BYTE;
            var bitsNeeded = 4 +
                mode.getCharacterCountBits(ZXing.QrCode.Internal.Version.getVersionForNumber(1)) +
                (Content.Length * 8);

            var version = ChooseVersion(bitsNeeded, out ecLevel);
            var ecBlocks = version.getECBlocksForLevel(ecLevel);
            var totalByteCapacity = version.TotalCodewords - ecBlocks.TotalECCodewords;
            var totalBitCapacity = totalByteCapacity << 3;

            // Write the mode marker (BYTE)
            bits.appendBits(mode.Bits, 4);

            // Write the number of bytes
            bits.appendBits(Content.Length, mode.getCharacterCountBits(version));

            // Write the bytes
            for (int i = 0; i < Content.Length; i++)
            {
                bits.appendBits(Content[i], 8);
            }

            // Terminate the bit stream
            //  - Write Termination Mode if space
            for (int i = 0; i < 4 && bits.Size < totalBitCapacity; ++i)
            {
                bits.appendBit(false);
            }

            // Add 8-bit alignment padding
            var bitsInLastByte = bits.Size & 0x07;
            if (bitsInLastByte > 0)
            {
                for (int i = bitsInLastByte; i < 8; i++)
                {
                    bits.appendBit(false);
                }
            }

            // Fill remain space with padding patterns
            var paddingBytes = totalByteCapacity - bits.SizeInBytes;
            for (int i = 0; i < paddingBytes; ++i)
            {
                bits.appendBits((i & 0x01) == 0 ? 0xEC : 0x11, 8);
            }

            // Interleave data bits with error correction code.
            var finalBits = interleaveWithECBytes(bits, version.TotalCodewords, totalByteCapacity, ecBlocks.NumBlocks);

            //  Choose the mask pattern and set to "qrCode".
            int dimension = version.DimensionForVersion;
            ByteMatrix matrix = new ByteMatrix(dimension, dimension);
            int maskPattern = chooseMaskPattern(finalBits, ecLevel, version, matrix);

            // Build the matrix and set it to "qrCode".
            MatrixUtil.buildMatrix(finalBits, ecLevel, version, maskPattern, matrix);

            // Render matrix to bytes
            return scaleMatrix(matrix.Array, Width, Height);
        }
Ejemplo n.º 5
0
 public void testNumBytes()
 {
    BitArray v = new BitArray();
    Assert.AreEqual(0, v.SizeInBytes);
    v.appendBit(false);
    // 1 bit was added in the vector, so 1 byte should be consumed.
    Assert.AreEqual(1, v.SizeInBytes);
    v.appendBits(0, 7);
    Assert.AreEqual(1, v.SizeInBytes);
    v.appendBits(0, 8);
    Assert.AreEqual(2, v.SizeInBytes);
    v.appendBits(0, 1);
    // We now have 17 bits, so 3 bytes should be consumed.
    Assert.AreEqual(3, v.SizeInBytes);
 }
Ejemplo n.º 6
0
      internal static BitArray stuffBits(BitArray bits, int wordSize)
      {
         var @out = new BitArray();

         // 1. stuff the bits
         int n = bits.Size;
         int mask = (1 << wordSize) - 2;
         for (int i = 0; i < n; i += wordSize)
         {
            int word = 0;
            for (int j = 0; j < wordSize; j++)
            {
               if (i + j >= n || bits[i + j])
               {
                  word |= 1 << (wordSize - 1 - j);
               }
            }
            if ((word & mask) == mask)
            {
               @out.appendBits(word & mask, wordSize);
               i--;
            }
            else if ((word & mask) == 0)
            {
               @out.appendBits(word | 1, wordSize);
               i--;
            }
            else
            {
               @out.appendBits(word, wordSize);
            }
         }

         // 2. pad last word to wordSize
         n = @out.Size;
         int remainder = n % wordSize;
         if (remainder != 0)
         {
            int j = 1;
            for (int i = 0; i < remainder; i++)
            {
               if (!@out[n - 1 - i])
               {
                  j = 0;
               }
            }
            for (int i = remainder; i < wordSize - 1; i++)
            {
               @out.appendBit(true);
            }
            @out.appendBit(j == 0);
         }
         return @out;
      }
Ejemplo n.º 7
0
      private static BitArray generateCheckWords(BitArray stuffedBits, int totalSymbolBits, int wordSize)
      {
         var messageSizeInWords = (stuffedBits.Size + wordSize - 1) / wordSize;
         for (var i = messageSizeInWords * wordSize - stuffedBits.Size; i > 0; i--)
         {
            stuffedBits.appendBit(true);
         }

         var rs = new ReedSolomonEncoder(getGF(wordSize));
         var totalSizeInFullWords = totalSymbolBits / wordSize;
         var messageWords = bitsToWords(stuffedBits, wordSize, totalSizeInFullWords);
         rs.encode(messageWords, totalSizeInFullWords - messageSizeInWords);

         var startPad = totalSymbolBits % wordSize;
         var messageBits = new BitArray();
         messageBits.appendBits(0, startPad);
         foreach (var messageWord in messageWords)
         {
            messageBits.appendBits(messageWord, wordSize);
         }
         return messageBits;
      }