Everyone seems to ignore the byte-order encoding of its expected result. The BitConverter class uses fixed encoding (usually Little-Endian, IIRC). The output in the example is assumed to be large-endian. In an ideal world, you just do the math yourself, but it's easier to use Array.Reverse and then use the built-in BitConverter class.
There will probably be a bunch of answers before I can post this, so here is a very quick piece of unsafe code:
public static unsafe ulong ToULong(byte[] values) { byte* buffer = stackalloc byte[8]; if (BitConverter.IsLittleEndian) Array.Reverse(values); System.Runtime.InteropServices.Marshal.Copy(values, 0, (IntPtr)buffer, values.Length); return *(ulong*)buffer; }
source share