Converts array of bytes to Int16, ignoring higher bits - CSharp System

CSharp examples for System:Byte Array

Description

Converts array of bytes to Int16, ignoring higher bits

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License"); 

public class Main{
        /// <summary>
        /// Converts array of bytes to Int16, ignoring higher bits
        /// </summary>
        /// <param name="msByte">Most significant byte</param>
        /// <param name="lsByte">Least significant byte</param>
        /// <param name="ignoreFirstBitsNum">Number of higher bits to ignore</param>
        /// <returns>Int16 integer.</returns>
        public static Int16 Int16FromBytes(byte msByte, byte lsByte, int ignoreFirstBitsNum)
        {//  ww w . j ava  2s . com
            if (ignoreFirstBitsNum > 0)
            {
                msByte &= (byte)(0xFF >> ignoreFirstBitsNum);
            }

            return (Int16)((msByte << 8) | lsByte);
        }
        /// <summary>
        /// Converts array of bytes to Int16
        /// </summary>
        /// <param name="msByte">Most significant byte</param>
        /// <param name="lsByte">Least significant byte</param>
        /// <returns>Int16 integer.</returns>
        public static Int16 Int16FromBytes(byte msByte, byte lsByte)
        {
            return Int16FromBytes(msByte, lsByte, 0);
        }
}

Related Tutorials