Converting Between Binary and Decimal in C#

.NET programming topics
Post Reply
Cyclops
Lieutenant
Lieutenant
Posts: 71
Joined: Wed Jul 15, 2009 1:48 pm
Location: London

Converting Between Binary and Decimal in C#

Post by Cyclops » Tue Nov 10, 2009 5:07 pm

The following two source code listings show how to write a crude but effective console mode application for converting a number, either from binary to decimal, or from decimal to binary. Note: you pass in the decimal number or the binary string as a parameter when typing the program's name at a command prompt.

From Decimal to Binary...

Code: Select all

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...

Code: Select all

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


Post Reply

Return to “.NET Programming”