/// <summary> /// Gets an Int64 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 int64 value</returns> public static long ToInt64(byte[] array, int startIndex, bool reverse = false) { if (array.Length - startIndex < 8) { throw new ArgumentException("Array must be at least of length 8"); } Int64Struct int64; if (reverse) { int64 = new Int64Struct() { 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 { int64 = new Int64Struct() { 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(int64.Int64); }
/// <summary> /// Gets the bytes from an Int64 /// </summary> /// <param name="value">int64 value</param> /// <returns>the bytes from the Int64</returns> public static byte[] GetBytes(long value, bool reverse = false) { Int64Struct int64 = new Int64Struct() { Int64 = value }; if (reverse) { return(new byte[] { int64.Byte7, int64.Byte6, int64.Byte5, int64.Byte4, int64.Byte3, int64.Byte2, int64.Byte1, int64.Byte0 }); } return(new byte[] { int64.Byte0, int64.Byte1, int64.Byte2, int64.Byte3, int64.Byte4, int64.Byte5, int64.Byte6, int64.Byte7 }); }