LitDev.LDMath.Base2Decimal C# (CSharp) Method

Base2Decimal() public static method

Convert a base number to a decimal integer.
public static Base2Decimal ( Primitive number, Primitive Base ) : Primitive
number Primitive The base number to convert (non negative).
Base Primitive The base to convert from (2 binary) (8 octal) (16 hex) or other bases up to 36.
return Primitive
        public static Primitive Base2Decimal(Primitive number, Primitive Base)
        {
            try
            {
                string sNumber = ((string)number).ToLower();
                int iBase = (int)Base;
                if (iBase < 2 || iBase > 36) return "FAILED";

                if (iBase == 2 || iBase == 8 || iBase == 16)
                {
                    return System.Convert.ToInt32(sNumber, iBase);
                }
                else
                {
                    var reversed = sNumber.Reverse();
                    decimal result = 0;
                    int pos = 0;
                    foreach (char c in reversed)
                    {
                        result += CharList.IndexOf(c) * (decimal)System.Math.Pow(iBase, pos);
                        pos++;
                    }
                    return result;
                }
            }
            catch (Exception ex)
            {
                Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                return "FAILED";
            }
        }