using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AMESCoreStudio.Web.Code
{

    public class Entire
    {
        /// <summary>
        /// 10進位與16進位互轉 Types = DecToHex/HexToDec
        /// </summary>
        /// <param name="InputTXT"></param>
        /// <param name="Types">10-16:DecToHex 16-10:HexToDec</param>
        /// <returns></returns>
        public string DecHex(string InputTXT, string Types)
        {
            // 10 to 16 
            if (Types == "DecToHex")
            {

                // To hold our converted unsigned integer32 value
                uint uiDecimal = 0;
                try
                {
                    // Convert text string to unsigned integer
                    uiDecimal = checked((uint)System.Convert.ToUInt32(InputTXT));
                }

                catch (System.OverflowException exception)
                {
                    // Show overflow message and return
                    return "Overflow" + exception.ToString();

                }

                // Format unsigned integer value to hex and show in another textbox
                return String.Format("{0:x2}", uiDecimal);
            }

            // 16 to 10
            else if (Types == "HexToDec")
            {


                // To hold our converted unsigned integer32 value
                uint uiHex = 0;

                try
                {
                    // Convert hex string to unsigned integer
                    uiHex = System.Convert.ToUInt32(InputTXT, 16);
                }

                catch (System.OverflowException exception)
                {
                    // Show overflow message and return
                    return "Overflow " + exception.ToString();

                }

                // Format it and show as a string
                return uiHex.ToString();


            }
            else
            {
                return "input Type ERROR";

            }

        }
    }
}