The program of conversion
1. Binary to decimal
2. Decimal to binary
3. Hex decimal to decimal
4. Decimal to hex decimal
5. Octal to decimal
6. Decimal to octal

Means at the user's request, for example, the introduction of No. 4
Convert from Decimal to hex decimal
Thus, the rest of the options, preferably without work ARRAYS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string ch;
            while (true)
            {
                Console.WriteLine("1 . convert from binary to decimal");
                Console.WriteLine("2 . convert from decimal to binary");
                Console.WriteLine("3 . convert from Hexdecimal to decimal");
                Console.WriteLine("4 . convert from decimal to hexdecimal");
                Console.WriteLine("5 . convert from octal to decimal");
                Console.WriteLine("6 . convert from decimal to octal");
                Console.WriteLine("7 . Exit");
                ch = Console.ReadLine();
                if (ch == "1")
                {
                    string binary;
                    Console.WriteLine("Enter the binary number");
                    binary = Console.ReadLine();
                    int power = 1;
                    int number = 0;
                    for (int i = binary.Length - 1; i <= 0; i--)
                    {
                        if (binary[i] == '1')
                            number = number + power;
                        power = power * 2;

                    }

                }
                Console.WriteLine("the decimal number is :");


            }
            if (ch == "2")
            {
                int Number;
                string binary = "";
                Console.Write("Enter the decimal number ");
                Number = Int32.Parse(Console.ReadLine());
                while (Number > 0)
                {
                    binary = Number % 2 + binary;
                    Number = Number / 2;

                }

                Console.WriteLine("the binary number is :");

            }
              else if (ch == "3")
            {
                break;
            }
        }
    }
}

your while loop will run forever i don see any break inside, sorry i didnte see that you are still in the proccess of coding till you reach number 7 but still you should redesign you code structure since you have a local variable which is unreachable in the if statement and i think that even if your number you won t get the expected result.

Edited 3 Years Ago by castajiz_2: none

I helped you and tried to figure out what you had in mind when doing it.Now you do the rest

 if (ch == "1")
                {
                    string binary;
                    Console.WriteLine("Enter the binary number");
                    binary = Console.ReadLine();
                    int power = 0;
                    int number = 0;
                    for (int i = binary.Length - 1; i >= 0; i--) // changed the ending state
                    {
                        if (binary[i] == '1')
                            number =  number + (int)Math.Pow(2, power); // tried to figure out what you had in mind
                        power++;

                    }
                    Console.WriteLine("the decimal number is :" + number);
                }

Well i suggest you again do some work and try a little bit harder, for instance, take a paper and do a conversion and then try to think how to code that. I know that it is a bit more difficult especially if you are a beginner but still. The whole point is that you think and think. Post the code again if you can t come up with a answer and im pretty sure that people will help you fast.

this new code please help me

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string ch;
            while (true)
            {
                Console.WriteLine("1 . convert from binary to decimal");
                Console.WriteLine("2 . convert from decimal to binary");
                Console.WriteLine("3 . convert from Hexdecimal to decimal");
                Console.WriteLine("4 . convert from decimal to hexdecimal");
                Console.WriteLine("5 . convert from octal to decimal");
                Console.WriteLine("6 . convert from decimal to octal");
                Console.WriteLine("7 . Exit");
                ch = Console.ReadLine();
                if (ch == "1")
                {
                    string binary;
                    Console.WriteLine("Enter the binary number");
                    binary = Console.ReadLine();
                    int power = 0;
                    int number = 0;
                    for (int i = binary.Length - 1; i >= 0; i--)
                    {
                        if (binary[i] == '1')
                            number = number + (int)Math.Pow(2, power);
                        power++;
                    }
                    Console.WriteLine("the decimal number is :" + number);
                }

                if (ch == "2")
                {
                    int Number;
                    string binary = "";
                    Console.Write("Enter the decimal number ");
                    Number = Int32.Parse(Console.ReadLine());
                    while (Number > 0)
                    {
                        binary = Number % 2 + binary;
                        Number = Number / 2;

                    }

                    Console.WriteLine("the binary number is :" + binary);
                    Console.WriteLine("\n");


                }
                if (ch == "3")
                {
                    int x = 12;
                    Console.WriteLine((char)(x + 55));
                    char y = 'f';
                    Console.WriteLine((int)y - 55);
                }
                string hexa;
                Console.WriteLine("\n");
                Console.WriteLine("Enter the hex decimal number");
                hexa = Console.ReadLine();
                int p = 1;
                int n = 0;

                for (int i = hexa.Length - 1; i <= 0; i--)
                {
                    if (hexa[i] < 'a')
                        n += p * (hexa[i] - 55);

                    else 
                    n += p * (hexa[i] - 48);




                }

            }
        }
    }
}

Well i can t help you right now, rushing to my class, i ll try to figure out what was your intention whith the ascii here,
by the way if you don t want make you own algorithms you can always use c# algorithms for example

Convert.ToString(Convert.ToInt32(hexavalue, 16), 10);

hex-dec

this final code all is true but convert Hex decimal to decimal not true


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string ch;
            while (true)
            {
                Console.WriteLine("1 . convert from binary to decimal");
                Console.WriteLine("2 . convert from decimal to binary");
                Console.WriteLine("3 . convert from decimal to hexdecimal");
                Console.WriteLine("4 . convert from Hexdecimal to decimal");
                Console.WriteLine("5 . convert from decimal to octal");
                Console.WriteLine("6 . convert from octal to decimal");
                Console.WriteLine("7 . Exit");
                ch = Console.ReadLine();
                if (ch == "1")
                {
                    string binary;
                    Console.Write("Enter the binary number");
                    binary = Console.ReadLine();
                    int power = 0;
                    int number = 0;
                    for (int i = binary.Length - 1; i >= 0; i--)
                    {
                        if (binary[i] == '1')
                            number = number + (int)Math.Pow(2, power);
                        power++;
                    }
                    Console.WriteLine("The decimal number is :" + number);
                }

                if (ch == "2")
                {
                    int Number;
                    string binary = "";
                    Console.Write("Enter the decimal number ");
                    Number = Int32.Parse(Console.ReadLine());
                    while (Number > 0)
                    {
                        binary = Number % 2 + binary;
                        Number = Number / 2;

                    }

                    Console.WriteLine("The binary number is :" + binary);
                    Console.WriteLine("\n");


                }
                if (ch == "3")
                {
                    int decimalNumber, quotient;
                    int i = 1, j, temp = 0;
                    char[] hexadecimalNumber = new char[100];

                    char temp1;

                    Console.WriteLine("Enter a Decimal Number :");

                    decimalNumber = int.Parse(Console.ReadLine());

                    quotient = decimalNumber;

                    while (quotient != 0)
                    {
                        temp = quotient % 16;

                        if (temp < 10)

                            temp = temp + 48;

                        else

                            temp = temp + 55;

                        temp1 = Convert.ToChar(temp);

                        hexadecimalNumber[i++] = temp1;

                        quotient = quotient / 16;
                    }
                    Console.Write("The HexaDecimal Number is ");

                    for (j = i - 1; j > 0; j--)

                        Console.Write(hexadecimalNumber[j]);





                }
                if (ch == "4")
                {
                    string hex;
                    Console.WriteLine("Enter a Hexdecimal Number :");
                    hex = Console.ReadLine();
                    int h = 0;
                    int decimall = 0;
                    for (int i = hex.Length - 1; i >= 0; i--)
                    {
                        if (hex[i] < 'a')
                            decimall = decimall + (int)Math.Pow(16, h);



                    }
                    Console.WriteLine(+decimall);




                }
                if (ch == "5")
                {

                    int decimalNumber, quotient, i = 1, j;

                    int[] octalNumber = new int[100];

                    Console.WriteLine("Enter a Decimal Number :");

                    decimalNumber = int.Parse(Console.ReadLine());

                    quotient = decimalNumber;

                    while (quotient != 0)
                    {

                        octalNumber[i++] = quotient % 8;

                        quotient = quotient / 8;

                    }

                    Console.WriteLine("The Octal Number is ");

                    for (j = i - 1; j > 0; j--)

                        Console.Write(octalNumber[j]);



                }
                if (ch == "6")
                {
                    int[] arr = new int[20];
                    int j, OctNum;
                    int i, power;
                    double DecNum = 0;
                    Console.WriteLine("Enter the Number: ");
                    OctNum = Int32.Parse(Console.ReadLine());

                    for (i = 0, j = 0; OctNum > 0; i++)
                    {
                        arr[i] = OctNum % 10;
                        OctNum = OctNum / 10;
                    }
                    for (power = 0, j = 0; j < i; j++, power++)
                    {
                        DecNum = DecNum + (arr[j] * Math.Pow(8.0, power));
                    }

                    Console.WriteLine("The Decimal form is {0}", DecNum);

                }

                if (ch == "7")
                {
                    break;

                }

            }


        }
    }
}

Ok,i decided to create this with my own code so here you go,

 if (ch == "4")
                {
                    string hex;
                    Console.WriteLine("Enter a Hexdecimal Number :");
                    hex = Console.ReadLine();
                    int buffer = 0;
                    int num2;
                    int result = 0; // result is your decimall
                    int counter = 10;
                    for (int i = hex.Length - 1; i >= 0; i--)
                    {
                        if (int.TryParse(hex[i].ToString(), out  num2))// if its a number 
                        {
                            result=result+ (int)(num2*Math.Pow(16,buffer));
                        }
                        else                                           //if it s a character
                        {
                            for (int j = 97; j < 103; j++)
                            {
                                if (hex[i] == (char)j) 
                                {
                                    result += counter * (int)Math.Pow(16, buffer);
                                    counter = 10;
                                    break;
                                }
                                counter++;
                            }
                         }
                        buffer++;
                        counter = 10;

                    }
                      Console.WriteLine(result);
                  }

i hope you ll be satisfied with it, you can t user uppercase letters if you want to use them make a method and code it by yourself, if you run into a problem feel free to make another thread. GOOD LUCK!

This article has been dead for over six months. Start a new discussion instead.