Is there a BitConverter
or other class that supports .Net Core that I can read integers and other values as having Big Endian encoding?
I don't feel good about needing to write a set of helper methods:
int GetBigEndianIntegerFromByteArray(byte[] data, int startIndex) {
return (data[startIndex] << 24)
| (data[startIndex + 1] << 16)
| (data[startIndex + 2] << 8)
| data[startIndex + 3];
}
Since .NET Core 2.1 there is a unified API for this in static class System.Buffers.Binary.BinaryPrimitives
It contains API for ReadOnlySpan and direct inverse endianness for primitive types (short/ushort,int/uint,long/ulong)
private void GetBigEndianIntegerFromByteArray(ReadOnlySpan<byte> span,int offset)
{
return BinaryPrimitives.ReadInt32BigEndian(span.Slice(offset));
}
System.Buffers.Binary.BinaryPrimitives class is a part of .NET Core 2.1 and no NuGet packages are needed
Also this class contains Try... methods
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With