using System;

namespace skyscraper5.Skyscraper.IO
{
    internal static class BcdDecoder
    {
        public static long UnpackBcd(this byte i)
        {
            return UnpackBcd(new byte[] { i });
        }
        public static long UnpackBcd(this ushort i)
        {
            return UnpackBcd(BitConverter.GetBytes(i));
        }
        public static long UnpackBcd(this uint i)
        {
            return UnpackBcd(BitConverter.GetBytes(i));
        }

        public static long UnpackBcd(byte[] buffer)
        {
            Array.Reverse(buffer);
            long result = 0;
            for (int i = 0; i < buffer.Length; i++)
            {
                result *= 10;
                int i1 = ((buffer[i] & 0xf0) >> 4);
                result += i1;
                result *= 10;
                int i2 = ((buffer[i] & 0x0f));
                result += i2;
            }
            return result;
        }
    }
}
