Returns a 32-bit signed integer converted from bytes in a Binary Coded Decimal format byte array. - CSharp System

CSharp examples for System:Byte

Description

Returns a 32-bit signed integer converted from bytes in a Binary Coded Decimal format byte array.

Demo Code


using System;//from ww  w  .  j  a  v a2  s .c  o m

public class Main{
        /// <summary>
        /// Returns a 32-bit signed integer converted from bytes in a Binary Coded Decimal format byte array.
        /// </summary>
        /// <param name="input">Input byte array to read from.</param>
        /// <param name="offset">Offset to start reading at.</param>
        /// <param name="length">Length of array to read.</param>
        public static int BCDToInt32(byte[] input, int offset, int length)
        {
            int result = 0;
            for (int i = offset; i < offset + length; i++)
            {
                byte p = input[i];
                result *= 100;
                result += 10 * (p >> 4);
                result += p & 0xf;
            }
            return result;
        }
}

Related Tutorials