Пример #1
0
        public void HexToNumbersConvertTest(int seed)
        {
            var random    = new Random(seed);
            var uint8     = unchecked ((byte)(random.Next() % byte.MaxValue));
            var uint16    = unchecked ((ushort)(random.Next() % ushort.MaxValue));
            var uint32    = unchecked ((uint)random.Next());
            var uint64    = unchecked ((ulong)random.NextDouble() * long.MaxValue);
            var uint8Hex  = BitConverter.ToString(new byte[] { uint8 }).Replace("-", "").ToLowerInvariant();
            var uint16Hex = BitConverter.ToString(BitConverter.GetBytes(uint16)).Replace("-", "").ToLowerInvariant();
            var uint32Hex = BitConverter.ToString(BitConverter.GetBytes(uint32)).Replace("-", "").ToLowerInvariant();
            var uint64Hex = BitConverter.ToString(BitConverter.GetBytes(uint64)).Replace("-", "").ToLowerInvariant();

            var hexBuffer = new char[256];

            // hex -> uint8
            Array.Copy(uint8Hex.ToCharArray(), hexBuffer, uint8Hex.Length);

            var actualUInt8 = HexConvert.ToUInt8(hexBuffer, 0);

            Assert.Equal(uint8, actualUInt8);

            actualUInt8 = HexConvert.ToUInt8(uint8Hex, 0);
            Assert.Equal(uint8, actualUInt8);

            Array.Clear(hexBuffer, 0, hexBuffer.Length);

            // hex -> uint16
            Array.Copy(uint16Hex.ToCharArray(), hexBuffer, uint16Hex.Length);

            var actualUInt16 = HexConvert.ToUInt16(hexBuffer, 0);

            Assert.Equal(uint16, actualUInt16);

            actualUInt16 = HexConvert.ToUInt16(uint16Hex, 0);
            Assert.Equal(uint16, actualUInt16);

            Array.Clear(hexBuffer, 0, hexBuffer.Length);

            // hex -> uint32
            Array.Copy(uint32Hex.ToCharArray(), hexBuffer, uint32Hex.Length);

            var actualUInt32 = HexConvert.ToUInt32(hexBuffer, 0);

            Assert.Equal(uint32, actualUInt32);

            actualUInt32 = HexConvert.ToUInt32(uint32Hex, 0);
            Assert.Equal(uint32, actualUInt32);

            Array.Clear(hexBuffer, 0, hexBuffer.Length);

            // hex -> uint64
            Array.Copy(uint64Hex.ToCharArray(), hexBuffer, uint64Hex.Length);

            var actualUInt64 = HexConvert.ToUInt64(hexBuffer, 0);

            Assert.Equal(uint64, actualUInt64);

            actualUInt64 = HexConvert.ToUInt64(uint64Hex, 0);
            Assert.Equal(uint64, actualUInt64);

            Array.Clear(hexBuffer, 0, hexBuffer.Length);
        }