/// <summary> /// Gets an UInt32 from the next 4 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 uint32 value</returns> public static uint ToUInt32(byte[] array, int startIndex, bool reverse = false) { if (array.Length - startIndex < 4) { throw new ArgumentException("Array must be at least of length 4"); } UInt32Struct uint32; if (reverse) { uint32 = new UInt32Struct() { Byte3 = array[startIndex], Byte2 = array[++startIndex], Byte1 = array[++startIndex], Byte0 = array[++startIndex] }; } else { uint32 = new UInt32Struct() { Byte0 = array[startIndex], Byte1 = array[++startIndex], Byte2 = array[++startIndex], Byte3 = array[++startIndex] }; } return(uint32.UInt32); }
/// <summary> /// Gets the bytes from an UInt32 /// </summary> /// <param name="value">uint32 value</param> /// <returns>the bytes from the UInt32</returns> public static byte[] GetBytes(uint value, bool reverse) { UInt32Struct uint32 = new UInt32Struct() { UInt32 = value }; if (reverse) { return(new byte[] { uint32.Byte3, uint32.Byte2, uint32.Byte1, uint32.Byte0 }); } return(new byte[] { uint32.Byte0, uint32.Byte1, uint32.Byte2, uint32.Byte3 }); }