I want to know how to make a program to convert from binary to decimal and from decimal to binary

Recommended Answers

All 7 Replies

In console apllication please

Google binary to decimal in C# you may find what you need.

Try first to write your solution in a draft, think how to code it, all of us know how to convert it theoretically, which means you won't pay a lot to think, and to code; it's for your interest to do it yourself, some lines of codes won't help you as they are not written by you!!

You have a huge number of ways to do it.

You could either tackle it as a human would, or, you look though the helpfile and see if theres a prebuilt way to do it.

Either way, get some ideas on paper, try and code it, and then when you have something... We'll help.

.NET even has a built-in function to do the conversation. Some basic research would tell you the answer

From Decimal to Binary..

using System;

class Program{

   static void Main(string[] args){

      try{
        
  int i = (int)Convert.ToInt64(args[0]);
         Console.WriteLine("\n{0} converted to Binary is {1}\n",i,ToBinary(i));
      
      }catch(Exception e){
        
         Console.WriteLine("\n{0}\n",e.Message);
 
      }

   }//end Main


  public static string ToBinary(Int64 Decimal)
  {
   // Declare a few variables we're going to need
   Int64 BinaryHolder;
   char[] BinaryArray;
   string BinaryResult = "";

   while (Decimal > 0)
   {
    BinaryHolder = Decimal % 2;
    BinaryResult += BinaryHolder;
    Decimal = Decimal / 2;
   }

   // The algoritm gives us the binary number in reverse order (mirrored)
   // We store it in an array so that we can reverse it back to normal
   BinaryArray = BinaryResult.ToCharArray();
   Array.Reverse(BinaryArray);
   BinaryResult = new string(BinaryArray);

   return BinaryResult;
  }


}//end class Program

From Binary to Decimal...

using System;

class Program{

   static void Main(string[] args){

      try{
        
         int i = ToDecimal(args[0]);
         Console.WriteLine("\n{0} converted to Decimal is {1}",args[0],i);
      
      }catch(Exception e){
        
         Console.WriteLine("\n{0}\n",e.Message);
 
      }

   }//end Main


                public static int ToDecimal(string bin)
  {
                    long l = Convert.ToInt64(bin,2);
                    int i = (int)l;
                    return i;
  }


}//end class Program

binary to decimal code -new-

private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
string sayii = textBox1.Text.Substring(0, textBox1.Text.Length);
uint[] sayicc = new uint[textBox1.Text.Length];


for (int i = 0; i < textBox1.Text.Length; i++)
{
sayicc = Convert.ToUInt32(sayii - 48);


}


uint[] dizi2 = new uint[textBox1.Text.Length];
uint bit = 1;


for (int j = 0; j < textBox1.Text.Length; j++)
{
dizi2[j] = bit;
bit *= 2;
}
Array.Reverse(dizi2);
uint deger = 0;
for (int i = 0; i <= dizi2.Length - 1; i++)
{
dizi2 *= sayicc;


if (sayicc == 1)
{
deger += dizi2;
}
}
textBox2.Text = "  " + deger.ToString();
}
else
{
MessageBox.Show("Deger girilecek alan boş.", "HATA!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.