示例#1
0
        public void TestBitArray()
        {
            BitArray bits = new BitArray(64);

            bits[0]  = true;
            bits[21] = true;
            byte[] bytes = new byte[8];
            bits.CopyTo(bytes, 0);

            //ulong dataValue = BitConverter.ToUInt64(bytes, 0) << 0;
            ulong dataValue = 1 << 20;

            Console.WriteLine($"{dataValue:x2}");
            Console.WriteLine($"{Convert.ToString((long) dataValue, 2)}");

            var stream = new MemoryStream();

            VarInt.WriteUInt64(stream, dataValue);

            byte[] array = stream.ToArray();
            Console.WriteLine($"{Package.HexDump(array)}");


            //var result = VarInt.ReadUInt64(new MemoryStream(array));
            var result = VarInt.ReadUInt64(new MemoryStream(new byte[] { 0x80, 0x80, 0x80, 0x11 }));

            Console.WriteLine($"{Convert.ToString((long) result, 2)}");

            //Assert.AreEqual(dataValue, result);
            Console.WriteLine($"{dataValue:x2}");

            //81808080808080808001                    €€€€€€€€.
        }
示例#2
0
        public void Add(ReadOnlySpan <byte> key, ReadOnlySpan <byte> data)
        {
            if (key == ReadOnlySpan <byte> .Empty || key == null || key.Length == 0)
            {
                throw new ArgumentException("Empty key");
            }
            //if (key == ReadOnlySpan<byte>.Empty || key == null || key.Length == 0) return;

            int sharedLen = 0;

            if (_restartCounter < 16)
            {
                sharedLen = CountSharedBytes(_lastKey, key);
            }
            else
            {
                _restarts.Add((uint)_stream.Position);
                _restartCounter = 0;
            }

            // An entry for a particular key/value pair has the form:
            //     shared_bytes: varint32
            VarInt.WriteUInt64(_stream, (ulong)sharedLen);
            //     unshared_bytes: varint32
            VarInt.WriteUInt64(_stream, (ulong)(key.Length - sharedLen));
            //     value_length: varint32
            VarInt.WriteUInt64(_stream, (ulong)data.Length);
            //     key_delta: char[unshared_bytes]
            _stream.Write(key.Slice(sharedLen));
            //     data: char[unshared_bytes]
            _stream.Write(data);

            _lastKey = key.ToArray();
            _restartCounter++;
        }
示例#3
0
 public void WriteUnsignedVarLong(long value)
 {
     // Need to fix this to ulong later
     VarInt.WriteUInt64(_buffer, (ulong)value);
 }