Beispiel #1
0
        /// <summary>
        /// Returns an 64-bit integer from a specific position in a byte array.
        /// </summary>
        /// <param name="x">The byte array.</param>
        /// <param name="offset">The offset to read the integer from.</param>
        /// <param name="endianness">The read order.</param>
        /// <returns></returns>
        public static long GetInt64(this byte[] x, int offset
                                    , DataTypes.Endianness endianness = DataTypes.Endianness.LittleEndian)
        {
            int result = -1;

            if (offset >= 0 && offset <= x.Count() - 8)
            {
                switch (endianness)
                {
                case DataTypes.Endianness.BigEndian:
                    result = x[offset] << 56 | x[offset + 1] << 48
                             | x[offset + 2] << 40 | x[offset + 3] << 32
                             | x[offset + 4] << 24 | x[offset + 5] << 16
                             | x[offset + 6] << 8 | x[offset + 7];
                    break;

                case DataTypes.Endianness.LittleEndian:
                    result = x[offset + 7] << 56 | x[offset + 6] << 48
                             | x[offset + 5] << 40 | x[offset + 4] << 32
                             | x[offset + 3] << 24 | x[offset + 2] << 16
                             | x[offset + 1] << 8 | x[offset];
                    break;
                }
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Returns an 16-bit integer from a specific position in a byte array.
        /// </summary>
        /// <param name="x">The byte array.</param>
        /// <param name="offset">The offset to read the integer from.</param>
        /// <param name="endianness">The read order.</param>
        /// <returns></returns>
        public static int GetInt16(this byte[] x, int offset
                                   , DataTypes.Endianness endianness = DataTypes.Endianness.LittleEndian)
        {
            int result = -1;

            if (offset >= 0 && offset <= x.Count() - 2)
            {
                switch (endianness)
                {
                case DataTypes.Endianness.BigEndian:
                    result = x[offset] << 8 | x[offset + 1];
                    break;

                case DataTypes.Endianness.LittleEndian:
                    result = x[offset + 1] << 8 | x[offset];
                    break;
                }
            }

            return(result);
        }