public static bool IsAlreadyDesiredEndianness(BinaryEndianness endianness) { return(endianness switch { BinaryEndianness.LittleEndian => BitConverter.IsLittleEndian, BinaryEndianness.BigEndian => !BitConverter.IsLittleEndian, _ => throw new NotImplementedException() });
public static byte ReadByte(byte[] buffer, int startIndex, BinaryEndianness endianness) { // I don't think this actually does anything since BinaryPrimitives.ReverseEndianness says it does nothing for bytes? byte value = buffer[startIndex]; return(IsAlreadyDesiredEndianness(endianness) ? value : BinaryPrimitives.ReverseEndianness(value)); }
public static Vector3 ReadVector3(byte[] buffer, int offset, BinaryEndianness endianness) { return(new Vector3( ReadFloat(buffer, offset, endianness), ReadFloat(buffer, offset + sizeof(float), endianness), ReadFloat(buffer, offset + sizeof(float) * 2, endianness) )); }
public static double ReadDouble(byte[] buffer, int startIndex, BinaryEndianness endianness) { long value = BitConverter.ToInt64(buffer, startIndex); return(BitConverter.Int64BitsToDouble(IsAlreadyDesiredEndianness(endianness) ? value : BinaryPrimitives.ReverseEndianness(value))); }
public static float ReadFloat(byte[] buffer, int startIndex, BinaryEndianness endianness) { int value = BitConverter.ToInt32(buffer, startIndex); return(BitConverter.Int32BitsToSingle(IsAlreadyDesiredEndianness(endianness) ? value : BinaryPrimitives.ReverseEndianness(value))); }
public static ushort ReadUInt16(byte[] buffer, int startIndex, BinaryEndianness endianness) { ushort value = BitConverter.ToUInt16(buffer, startIndex); return(IsAlreadyDesiredEndianness(endianness) ? value : BinaryPrimitives.ReverseEndianness(value)); }