-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
We currently have overloads to read short, int, long, etc for big and little endian. However, we don't have overloads for float and double. We should add them to complete the types that are supported by this class.
namespace System.Buffers.Binary
{
public static class BinaryPrimitives
{
public static float ReadSingleBigEndian(ReadOnlySpan<byte> buffer);
public static float ReadSingleLittleEndian(ReadOnlySpan<byte> buffer);
public static double ReadDoubleBigEndian(ReadOnlySpan<byte> buffer);
public static double ReadDoubleLittleEndian(ReadOnlySpan<byte> buffer);
public static bool TryReadSingleBigEndian(ReadOnlySpan<byte> buffer, out float value);
public static bool TryReadSingleLittleEndian(ReadOnlySpan<byte> buffer, out float value);
public static bool TryReadDoubleBigEndian(ReadOnlySpan<byte> buffer, out double value);
public static bool TryReadDoubleLittleEndian(ReadOnlySpan<byte> buffer, out double value);
public static bool TryWriteSingleBigEndian(System.Span<byte> destination, float value);
public static bool TryWriteSingleLittleEndian(System.Span<byte> destination, float value);
public static bool TryWriteDoubleBigEndian(System.Span<byte> destination, double value);
public static bool TryWriteDoubleLittleEndian(System.Span<byte> destination, double value);
public static void WriteSingleBigEndian(System.Span<byte> destination, float value);
public static void WriteSingleLittleEndian(System.Span<byte> destination, float value);
public static void WriteDoubleBigEndian(System.Span<byte> destination, double value);
public static void WriteDoubleLittleEndian(System.Span<byte> destination, double value);
}
}This API is useful when you are reading/writing to a memory stream that you know is little or big endian. For example, it would have been useful in the following code in the C# implementation of Google FlatBuffers:
Write float/double LittleEndian
https://github.com/google/flatbuffers/blob/1c7d91cc55a9deb05e7ea93ba10b5ab511d29238/net/FlatBuffers/ByteBuffer.cs#L509-L547
Read float/double LittleEndian
https://github.com/google/flatbuffers/blob/1c7d91cc55a9deb05e7ea93ba10b5ab511d29238/net/FlatBuffers/ByteBuffer.cs#L710-L750
A possible workaround is to make 2 calls like the following:
return BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(buffer));@stephentoub @jkotas @ahsonkhan @GrabYourPitchforks @tannergooding