Exemple #1
0
        /// <summary>
        /// Gets an UInt16 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 uint16 value</returns>
        public static ushort ToUInt16(byte[] array, int startIndex, bool reverse = false)
        {
            if (array.Length - startIndex < 2)
            {
                throw new ArgumentException("Array must be at least of length 2");
            }

            UInt16Struct uint16;

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

            return(uint16.UInt16);
        }
Exemple #2
0
        /// <summary>
        /// Gets the bytes from an UInt16
        /// </summary>
        /// <param name="value">uint16 value</param>
        /// <returns>the bytes from the UInt16</returns>
        public static byte[] GetBytes(ushort value, bool reverse = false)
        {
            UInt16Struct uint16 = new UInt16Struct()
            {
                UInt16 = value
            };

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

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