Exemplo n.º 1
0
        /// <summary>
        /// Gets an Int16 from the next 2 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 int16 value</returns>
        public static short ToInt16(byte[] array, int startIndex, bool reverse = false)
        {
            if (array.Length - startIndex < 2)
            {
                throw new ArgumentException("Array must be at least of length 2");
            }

            Int16Struct int16;

            if (reverse)
            {
                int16 = new Int16Struct()
                {
                    Byte1 = array[startIndex], Byte0 = array[++startIndex]
                };
            }
            else
            {
                int16 = new Int16Struct()
                {
                    Byte0 = array[startIndex], Byte1 = array[++startIndex]
                };
            }

            return(int16.Int16);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the bytes from an Int16
        /// </summary>
        /// <param name="value">int16 value</param>
        /// <param name="reverse">reverse the resulting byte array</param>
        /// <returns>the bytes from the Int16</returns>
        public static byte[] GetBytes(short value, bool reverse = false)
        {
            Int16Struct int16 = new Int16Struct()
            {
                Int16 = value
            };

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

            return(new byte[] { int16.Byte0, int16.Byte1 });
        }