Click here to Skip to main content
15,906,645 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I would to convert it a decimal number to any base.
In case of Hex, I want to express the Hex numbers correctly, like A instead of 10, B instead of 11, etc.
C#
using System;
using System.Collections;
namespace csstack
{
    class Class1
    {
        static void Main(string[] args)
        {
            int num, baseNum;
            string again = "y"; //do it again
            Console.WriteLine();
            while ((again == "y") || (again == "Y"))
            {
                Console.Write("Enter a decimal ynumber: ");
                num = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter a base: ");
                baseNum = Convert.ToInt32(Console.ReadLine());
                Console.Write(num + " converts to ");
                MulBase(num, baseNum);
                Console.WriteLine(" in Base-" + baseNum);
                Console.Write("\nDo it again? Anwer 'y' or 'N':\t");
                again = Console.ReadLine();

            }
            Console.WriteLine();
        }
        
        static void MulBase(int n, int b)
        {
            Stack digits = new Stack();
            do
            {
                digits.Push(n % b);
                n /= b;
            } while (n != 0);
            while (digits.Count > 0)
                Console.Write(digits.Pop()); 
        }
    }
}

This is what I did so far. Could anyone help me?
Posted
Updated 19-Oct-13 20:19pm
v3

Though you not mentioned what problem you are facing within the code but if you look at these links you can surely do it:
how to convert from decimal to any base in c#?[^]
Convert decimal numbers to any base (C#)[^]
Quickest way to convert a base 10 number to any base in .NET[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Oct-13 2:26am    
Sure, a 5.
—SA
ridoy 20-Oct-13 2:28am    
Thank you,SA.:)
There is no such thing as conversion, in this sense. All you are doing is taking binary numbers and displaying them in different base representations, which can be done by simply using the standard library methods String.Format() or int.ToString().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900