class
Any2Dec
{
public static void Main()
{
try
{
Console.Write("Enter
the number base: ");
string baseData = Console.ReadLine();
int numberBase = int.Parse(baseData,
NumberStyles.Integer);
Console.Write("Enter
the number: ");
string data = Console.ReadLine().ToUpperInvariant();
Console.WriteLine(GetAny2Dec(numberBase, data));
}
catch(Exception
ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
/*Xp=An*P^(n-1) + An-1*P^(n-2)+An-2*P^(n-3)+…+A2P^1 +
A1P^0*/
public static long GetAny2Dec(int
numberBase, string data)
{
const string ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
long result = 0;
for (int i =0; i <
data.Length; i++)
{
result
+= ABC.IndexOf(data[i]) * ((long)Math.Pow(numberBase
, data.Length – i – 1));
}
return result;
}
}