Any2Dec

                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;

                               }

                }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s