public static void HalfToInt16Bits() { Half input = (Half)12.34; short result = BitConverter.HalfToInt16Bits(input); Assert.Equal((short)18988, result); Half roundtripped = BitConverter.Int16BitsToHalf(result); Assert.Equal(input, roundtripped); }
public static bool TryReadHalfLittleEndian(ReadOnlySpan <byte> source, out Half value) { if (!BitConverter.IsLittleEndian) { bool success = MemoryMarshal.TryRead(source, out short tmp); value = BitConverter.Int16BitsToHalf(ReverseEndianness(tmp)); return(success); } return(MemoryMarshal.TryRead(source, out value)); }
public virtual Half ReadHalf() => BitConverter.Int16BitsToHalf(BinaryPrimitives.ReadInt16LittleEndian(InternalRead(2)));
public static Half ReadHalfLittleEndian(ReadOnlySpan <byte> source) { return(!BitConverter.IsLittleEndian ? BitConverter.Int16BitsToHalf(ReverseEndianness(MemoryMarshal.Read <short>(source))) : MemoryMarshal.Read <Half>(source)); }
public void SpanWrite() { Assert.True(BitConverter.IsLittleEndian); Span <byte> span = new byte[8]; byte byteValue = 0x11; MemoryMarshal.Write <byte>(span, ref byteValue); TestHelpers.Validate <byte>(span, byteValue); Assert.True(MemoryMarshal.TryWrite <byte>(span, ref byteValue)); TestHelpers.Validate <byte>(span, byteValue); sbyte sbyteValue = 0x11; MemoryMarshal.Write <sbyte>(span, ref sbyteValue); TestHelpers.Validate <sbyte>(span, sbyteValue); Assert.True(MemoryMarshal.TryWrite <sbyte>(span, ref sbyteValue)); TestHelpers.Validate <sbyte>(span, sbyteValue); ushort ushortValue = 0x1122; MemoryMarshal.Write <ushort>(span, ref ushortValue); TestHelpers.Validate <ushort>(span, ushortValue); Assert.True(MemoryMarshal.TryWrite <ushort>(span, ref ushortValue)); TestHelpers.Validate <ushort>(span, ushortValue); uint uintValue = 0x11223344; MemoryMarshal.Write <uint>(span, ref uintValue); TestHelpers.Validate <uint>(span, uintValue); Assert.True(MemoryMarshal.TryWrite <uint>(span, ref uintValue)); TestHelpers.Validate <uint>(span, uintValue); ulong ulongValue = 0x1122334455667788; MemoryMarshal.Write <ulong>(span, ref ulongValue); TestHelpers.Validate <ulong>(span, ulongValue); Assert.True(MemoryMarshal.TryWrite <ulong>(span, ref ulongValue)); TestHelpers.Validate <ulong>(span, ulongValue); short shortValue = 0x1122; MemoryMarshal.Write <short>(span, ref shortValue); TestHelpers.Validate <short>(span, shortValue); Assert.True(MemoryMarshal.TryWrite <short>(span, ref shortValue)); TestHelpers.Validate <short>(span, shortValue); int intValue = 0x11223344; MemoryMarshal.Write <int>(span, ref intValue); TestHelpers.Validate <int>(span, intValue); Assert.True(MemoryMarshal.TryWrite <int>(span, ref intValue)); TestHelpers.Validate <int>(span, intValue); long longValue = 0x1122334455667788; MemoryMarshal.Write <long>(span, ref longValue); TestHelpers.Validate <long>(span, longValue); Assert.True(MemoryMarshal.TryWrite <long>(span, ref longValue)); TestHelpers.Validate <long>(span, longValue); Half halfValue = BitConverter.Int16BitsToHalf(0x1122); MemoryMarshal.Write <Half>(span, ref halfValue); TestHelpers.Validate <Half>(span, halfValue); Assert.True(MemoryMarshal.TryWrite <Half>(span, ref halfValue)); TestHelpers.Validate <Half>(span, halfValue); float floatValue = BitConverter.Int32BitsToSingle(0x11223344); MemoryMarshal.Write <float>(span, ref floatValue); TestHelpers.Validate <float>(span, floatValue); Assert.True(MemoryMarshal.TryWrite <float>(span, ref floatValue)); TestHelpers.Validate <float>(span, floatValue); double doubleValue = BitConverter.Int64BitsToDouble(0x1122334455667788); MemoryMarshal.Write <double>(span, ref doubleValue); TestHelpers.Validate <double>(span, doubleValue); Assert.True(MemoryMarshal.TryWrite <double>(span, ref doubleValue)); TestHelpers.Validate <double>(span, doubleValue); }
public void SpanRead() { ulong value; // [11 22 33 44 55 66 77 88] if (BitConverter.IsLittleEndian) { value = 0x8877665544332211; } else { value = 0x1122334455667788; } Span <byte> span; unsafe { span = new Span <byte>(&value, 8); } Assert.Equal <byte>(0x11, MemoryMarshal.Read <byte>(span)); Assert.True(MemoryMarshal.TryRead(span, out byte byteValue)); Assert.Equal(0x11, byteValue); Assert.Equal <sbyte>(0x11, MemoryMarshal.Read <sbyte>(span)); Assert.True(MemoryMarshal.TryRead(span, out byte sbyteValue)); Assert.Equal(0x11, byteValue); Assert.Equal <ushort>(0x1122, ReadUInt16BigEndian(span)); Assert.True(TryReadUInt16BigEndian(span, out ushort ushortValue)); Assert.Equal(0x1122, ushortValue); Assert.Equal <ushort>(0x2211, ReadUInt16LittleEndian(span)); Assert.True(TryReadUInt16LittleEndian(span, out ushortValue)); Assert.Equal(0x2211, ushortValue); Assert.Equal <short>(0x1122, ReadInt16BigEndian(span)); Assert.True(TryReadInt16BigEndian(span, out short shortValue)); Assert.Equal(0x1122, shortValue); Assert.Equal <short>(0x2211, ReadInt16LittleEndian(span)); Assert.True(TryReadInt16LittleEndian(span, out shortValue)); Assert.Equal(0x2211, ushortValue); Assert.Equal <uint>(0x11223344, ReadUInt32BigEndian(span)); Assert.True(TryReadUInt32BigEndian(span, out uint uintValue)); Assert.Equal <uint>(0x11223344, uintValue); Assert.Equal <uint>(0x44332211, ReadUInt32LittleEndian(span)); Assert.True(TryReadUInt32LittleEndian(span, out uintValue)); Assert.Equal <uint>(0x44332211, uintValue); Assert.Equal <int>(0x11223344, ReadInt32BigEndian(span)); Assert.True(TryReadInt32BigEndian(span, out int intValue)); Assert.Equal <int>(0x11223344, intValue); Assert.Equal <int>(0x44332211, ReadInt32LittleEndian(span)); Assert.True(TryReadInt32LittleEndian(span, out intValue)); Assert.Equal <int>(0x44332211, intValue); Assert.Equal <ulong>(0x1122334455667788, ReadUInt64BigEndian(span)); Assert.True(TryReadUInt64BigEndian(span, out ulong ulongValue)); Assert.Equal <ulong>(0x1122334455667788, ulongValue); Assert.Equal <ulong>(0x8877665544332211, ReadUInt64LittleEndian(span)); Assert.True(TryReadUInt64LittleEndian(span, out ulongValue)); Assert.Equal <ulong>(0x8877665544332211, ulongValue); Assert.Equal <long>(0x1122334455667788, ReadInt64BigEndian(span)); Assert.True(TryReadInt64BigEndian(span, out long longValue)); Assert.Equal <long>(0x1122334455667788, longValue); Assert.Equal <long>(unchecked ((long)0x8877665544332211), ReadInt64LittleEndian(span)); Assert.True(TryReadInt64LittleEndian(span, out longValue)); Assert.Equal <long>(unchecked ((long)0x8877665544332211), longValue); Half expectedHalf = BitConverter.Int16BitsToHalf(0x1122); Assert.Equal <Half>(expectedHalf, ReadHalfBigEndian(span)); Assert.True(TryReadHalfBigEndian(span, out Half halfValue)); Assert.Equal <Half>(expectedHalf, halfValue); expectedHalf = BitConverter.Int16BitsToHalf(0x2211); Assert.Equal <Half>(expectedHalf, ReadHalfLittleEndian(span)); Assert.True(TryReadHalfLittleEndian(span, out halfValue)); Assert.Equal <Half>(expectedHalf, halfValue); float expectedFloat = BitConverter.Int32BitsToSingle(0x11223344); Assert.Equal <float>(expectedFloat, ReadSingleBigEndian(span)); Assert.True(TryReadSingleBigEndian(span, out float floatValue)); Assert.Equal <float>(expectedFloat, floatValue); expectedFloat = BitConverter.Int32BitsToSingle(0x44332211); Assert.Equal <float>(expectedFloat, ReadSingleLittleEndian(span)); Assert.True(TryReadSingleLittleEndian(span, out floatValue)); Assert.Equal <float>(expectedFloat, floatValue); double expectedDouble = BitConverter.Int64BitsToDouble(0x1122334455667788); Assert.Equal <double>(expectedDouble, ReadDoubleBigEndian(span)); Assert.True(TryReadDoubleBigEndian(span, out double doubleValue)); Assert.Equal <double>(expectedDouble, doubleValue); expectedDouble = BitConverter.Int64BitsToDouble(unchecked ((long)0x8877665544332211)); Assert.Equal <double>(expectedDouble, ReadDoubleLittleEndian(span)); Assert.True(TryReadDoubleLittleEndian(span, out doubleValue)); Assert.Equal <double>(expectedDouble, doubleValue); }