Example #1
0
        public void TestEncode()
        {
            VariableLengthInteger n = new VariableLengthInteger((UInt64)61);

            Assert.AreEqual((UInt64)61, n.Value);
            Assert.AreEqual(8, n.Size);
            byte[] b = n.Encode();
            Assert.AreEqual(1, b.Length);
            Assert.AreEqual(61, b[0]);

            n.Value = 1414;
            Assert.AreEqual((UInt64)1414, n.Value);
            Assert.AreEqual(16, n.Size);
            b = n.Encode();
            Assert.AreEqual(2, b.Length);
            Assert.AreEqual(0x40, b[0] & 0xc0);
            Assert.AreEqual(0x05, b[0] % 0x40); // 1024 + 256
            Assert.AreEqual(134, b[1]);

            n.Value = 65536;
            Assert.AreEqual((UInt64)65536, n.Value);
            Assert.AreEqual(32, n.Size);
            b = n.Encode();
            Assert.AreEqual(4, b.Length);
            Assert.AreEqual(0x80, b[0] & 0xc0);
            Assert.AreEqual(0, b[0] % 0x40);
            Assert.AreEqual(1, b[1]);
            Assert.AreEqual(0, b[2]);
            Assert.AreEqual(0, b[3]);

            n.Value = (1UL << 32);
            Assert.AreEqual(1UL << 32, n.Value);
            Assert.AreEqual(64, n.Size);
            b = n.Encode();
            Assert.AreEqual(8, b.Length);
            Assert.AreEqual(0xc0, b[0] & 0xc0);
            Assert.AreEqual(0, b[0] % 0x40);
            Assert.AreEqual(0, b[1]);
            Assert.AreEqual(0, b[2]);
            Assert.AreEqual(1, b[3]);
            Assert.AreEqual(0, b[4]);
            Assert.AreEqual(0, b[5]);
            Assert.AreEqual(0, b[6]);
            Assert.AreEqual(0, b[7]);

            n.Value = (UInt64)151288809941952652;
            Assert.AreEqual(64, n.Size);
            b = n.Encode();
            Assert.AreEqual(8, b.Length);
            Assert.AreEqual(0xc2, b[0]);
            Assert.AreEqual(0x19, b[1]);
            Assert.AreEqual(0x7c, b[2]);
            Assert.AreEqual(0x5e, b[3]);
            Assert.AreEqual(0xff, b[4]);
            Assert.AreEqual(0x14, b[5]);
            Assert.AreEqual(0xe8, b[6]);
            Assert.AreEqual(0x8c, b[7]);
        }
Example #2
0
        /// <summary>
        /// Encode a StreamFrame to a raw byte array
        /// </summary>
        /// <returns>The encoded frame</returns>
        public override byte[] Encode()
        {
            List <byte> content = new List <byte>();

            content.Add(Type);
            content.AddRange(StreamID.Encode());
            if (OFF)
            {
                content.AddRange(Offset.Encode());
            }
            if (LEN)
            {
                content.AddRange(_length.Encode());
            }
            content.AddRange(Data);

            return(content.ToArray());
        }