コード例 #1
0
        /// <summary>
        /// Gets an Int32 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 int32 value</returns>
        public static int ToInt32(byte[] array, int startIndex, bool reverse = false)
        {
            if (array.Length - startIndex < 4)
            {
                throw new ArgumentException("Array must be at least of length 4");
            }

            Int32Struct int32;

            if (reverse)
            {
                int32 = new Int32Struct()
                {
                    Byte3 = array[startIndex], Byte2 = array[++startIndex], Byte1 = array[++startIndex], Byte0 = array[++startIndex]
                };
            }
            else
            {
                int32 = new Int32Struct()
                {
                    Byte0 = array[startIndex], Byte1 = array[++startIndex], Byte2 = array[++startIndex], Byte3 = array[++startIndex]
                };
            }

            return(int32.Int32);
        }
コード例 #2
0
        /// <summary>
        /// Gets the bytes from an Int32
        /// </summary>
        /// <param name="value">int32 value</param>
        /// <returns>the bytes from the Int32</returns>
        public static byte[] GetBytes(int value, bool reverse = false)
        {
            Int32Struct int32 = new Int32Struct()
            {
                Int32 = value
            };

            if (reverse)
            {
                return(new byte[] { int32.Byte3, int32.Byte2, int32.Byte1, int32.Byte0 });
            }

            return(new byte[] { int32.Byte0, int32.Byte1, int32.Byte2, int32.Byte3 });
        }