Reads an unsigned 16 bit integer from an array coming from a device, and returns it as an 'int' - CSharp System

CSharp examples for System:Array Element

Description

Reads an unsigned 16 bit integer from an array coming from a device, and returns it as an 'int'

Demo Code

// Copyright (c) The Android Open Source Project, Ryan Conrad, Quamotion. All rights reserved.

public class Main{
        /// <summary>
        /// Reads an unsigned 16 bit integer from an array coming from a device,
        /// and returns it as an 'int'
        /// </summary>
        /// <param name="value">the array containing the 16 bit int (2 byte).</param>
        /// <param name="offset">the offset in the array at which the int starts</param>
        /// <remarks>Array length must be at least offset + 2</remarks>
        /// <returns>the integer read from the array.</returns>
        public static int SwapU16BitFromArray(this byte[] value, int offset)
        {/*from  w w w  . j a  v a2s . c o  m*/
            int v = 0;
            v |= ((int)value[offset]) & 0x000000FF;
            v |= (((int)value[offset + 1]) & 0x000000FF) << 8;

            return v;
        }
}

Related Tutorials