/// <summary> /// Gets an UInt64 from the next 8 bytes in a byte array, starting at the specified index /// </summary> /// <param name="array">byte array</param> /// <param name="startIndex">start index</param> /// <param name="reverse">reverse the byte array, useful to quickly switch endiannes</param> /// <returns>the uint64 value</returns> public static ulong ToUInt64(byte[] array, int startIndex, bool reverse = false) { if (array.Length - startIndex < 8) { throw new ArgumentException("Array must be at least of length 8"); } UInt64Struct uint64; if (reverse) { uint64 = new UInt64Struct() { Byte7 = array[startIndex], Byte6 = array[++startIndex], Byte5 = array[++startIndex], Byte4 = array[++startIndex], Byte3 = array[++startIndex], Byte2 = array[++startIndex], Byte1 = array[++startIndex], Byte0 = array[++startIndex] }; } else { uint64 = new UInt64Struct() { Byte0 = array[startIndex], Byte1 = array[++startIndex], Byte2 = array[++startIndex], Byte3 = array[++startIndex], Byte4 = array[++startIndex], Byte5 = array[++startIndex], Byte6 = array[++startIndex], Byte7 = array[++startIndex] }; } return(uint64.UInt64); }
/// <summary> /// Gets the bytes from an UInt64 /// </summary> /// <param name="value">uint64 value</param> /// <returns>the bytes from the UInt64</returns> public static byte[] GetBytes(ulong value, bool reverse = false) { UInt64Struct uint64 = new UInt64Struct() { UInt64 = value }; if (reverse) { return(new byte[] { uint64.Byte7, uint64.Byte6, uint64.Byte5, uint64.Byte4, uint64.Byte3, uint64.Byte2, uint64.Byte1, uint64.Byte0 }); } return(new byte[] { uint64.Byte0, uint64.Byte1, uint64.Byte2, uint64.Byte3, uint64.Byte4, uint64.Byte5, uint64.Byte6, uint64.Byte7 }); }