示例#1
0
        public void UInt16LittleEndianTest()
        {
            int    bufferOffset      = 1;
            UInt16 bufferSourceValue = 0x1234;
            var    bufferSource      = new byte[] { 0xFF, 0x34, 0x12, 0xFF };

            // Test ToUInt
            UInt16 value = bufferSource.ToUInt16(bufferOffset, EndianFormat.Little);

            value.ShouldBe(bufferSourceValue);

            // Test ToBuffer
            var bufferOutput        = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF };
            var bufferOutputSegment = new ArraySegment <byte>(bufferOutput, bufferOffset, bufferOutput.Length - bufferOffset);

            value.ToBuffer(bufferOutputSegment, endian: EndianFormat.Little).Array.ShouldBe(bufferSource);
            value.ToBuffer(bufferOutput.Fill(0xFF), bufferOffset, EndianFormat.Little).ShouldBe(bufferSource);
            value.ToBufferNew(EndianFormat.Little).ShouldBe(new byte[] { 0x34, 0x12 });
        }
示例#2
0
        public void UInt16BigEndianTest()
        {
            int    bufferOffset      = 2;
            UInt16 bufferSourceValue = 0x1234;
            var    bufferSource      = new byte[] { 0xFF, 0xFF, 0x12, 0x34, 0xFF, 0xFF };

            // Test ToUInt
            UInt16 value = bufferSource.ToUInt16(bufferOffset);

            value.ShouldBe(bufferSourceValue);

            // Test ToBuffer
            var bufferOutput        = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
            var bufferOutputSegment = new ArraySegment <byte>(bufferOutput, bufferOffset, bufferOutput.Length - bufferOffset);

            value.ToBuffer(bufferOutputSegment).Array.ShouldBe(bufferSource);
            value.ToBuffer(bufferOutput.Fill(0xFF), startOffset: bufferOffset).ShouldBe(bufferSource);
            value.ToBufferNew().ShouldBe(new byte[] { 0x12, 0x34 });
        }