private static void RoundTripInt32(int num, params byte[] bytes)
        {
            byte[] buffer = NetworkOrderBitsConverter.GetBytes(num);

            Assert.Equal(4, buffer.Length);
            Assert.Equal(bytes, buffer);

            Assert.Equal(num, NetworkOrderBitsConverter.ToInt32(buffer));

            NetworkOrderBitsConverter.PutInt32(num, buffer);

            Assert.Equal(bytes, buffer);

            Assert.Equal(num, NetworkOrderBitsConverter.ToInt32(buffer));
        }
        public void TestInt64()
        {
            byte[] buffer = NetworkOrderBitsConverter.GetBytes(1);

            Assert.AreEqual(1, buffer[3]);
            Assert.AreEqual(0, buffer[0]);

            long num = NetworkOrderBitsConverter.ToInt32(buffer);

            Assert.AreEqual(1, num);

            NetworkOrderBitsConverter.PutInt32(16777216, buffer, 0);

            Assert.AreEqual(1, buffer[0]);
            Assert.AreEqual(0, buffer[3]);

            num = NetworkOrderBitsConverter.ToInt32(buffer);

            Assert.AreEqual(16777216, num);
        }
Пример #3
0
        protected int AddProperty(Span <byte> output, string name, byte[] value)
        {
            if (name.Length > 255)
            {
                throw new ArgumentException("property name length exceed maximum size");
            }

            int totalLength = GetPropertyLength(name, value.Length);

            if (totalLength > output.Length)
            {
                throw new Exception("totalLength of property exceed maximum size");
            }

            output[0] = (byte)name.Length;
            output    = output.Slice(NameLengthSize);
            System.Text.Encoding.ASCII.GetBytes(name, output);
            output = output.Slice(name.Length);
            NetworkOrderBitsConverter.PutInt32(value.Length, output);
            output = output.Slice(ValueLengthSize);
            value.CopyTo(output);

            return(totalLength);
        }
Пример #4
0
        protected int AddProperty(byte[] output, int outputOffset, string name, byte[] value)
        {
            if (name.Length > 255)
            {
                throw new ArgumentException("property name length exceed maximum size");
            }

            int totalLength = GetPropertyLength(name, value.Length);

            if (totalLength > (output.Length - outputOffset))
            {
                throw new Exception("totalLength of property exceed maximum size");
            }

            output[outputOffset] = (byte)name.Length;
            outputOffset        += NameLengthSize;
            System.Text.Encoding.ASCII.GetBytes(name, 0, name.Length, output, outputOffset);
            outputOffset += name.Length;
            NetworkOrderBitsConverter.PutInt32(value.Length, output, outputOffset);
            outputOffset += ValueLengthSize;
            Buffer.BlockCopy(value, 0, output, outputOffset, value.Length);

            return(totalLength);
        }