With the help of various sources, I have written some SwapBytes
methods in my binary reader class that swap endian in ushort
, uint
, an
Just add a cast to unsigned at the start and back to signed at the end.
public long SwapBytes(long value)
{
return (long)SwapBytes((ulong)value);
}
It might be necessary to manually inline the call to SwapBytes
for maximal performance.
On a different note, you might want to avoid swapping, in favour of directly reading the data from the original byte array in the desired endianness. See Efficient way to read big endian data in C# for details.