Im trying to cast a 4 byte array to an ulong in C#. I'm currently using this code:
atomSize = BitConverter.ToUInt32(buffer, 0);
The byte[4] contains this:
0 0 0 32
However, the bytes are Big-Endian. Is there a simple way to convert this Big-Endian ulong to a Little-Endian ulong?
I believe that the EndianBitConverter in Jon Skeet's MiscUtil library (nuget link) can do what you want.
You could also swap the bits using bit shift operations:
Usage:
System.Net.IPAddress.NetworkToHostOrder(atomSize);
will flip your bytes.I recommend using Mono's
DataConvert
which is likeBitConverter
on steroids. It allows you to read in big-endian byte arrays directly and improves massively onBitConverter
.A direct link to the source is here.
This may be old but I'm surprised no one came up with this simplest answer yet, only requires one line...
I'm using it to compare checksums generated in C# (little-endian) with checksums generated in Java (big-endian).
No?