Converting from decimal to binary and back

This tutorial will show you how to convert from binary system (base 2) to decimal system (base 10) and back from decimal to binary. It provides a sample .NET 2.0 application which performs these conversions.


You can download this project. It is developed in Visual Studio .NET 2005, using .NET Framework 2.0. However, the code can be used in .NET Framework 1.1 without any changes.

Here is how the application looks like in Windows Vista:

For more information on the binary numbering system read the Understanding Binary tutorial.

Converting from decimal to binary
Converting from decimal to binary involves a small algorithm that we need to use. There are multiple ways of converting an integer to binary, but I believe this one is the simplest to understand and implement:



private 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;

}


Converting from binary to decimal
Conversion from binary to decimal is done even easier. We don’t need to use an algorithm because the .NET Framework provides us with a method called Convert.ToInt64() to which you pass a decimal.

You can see in the Binary Convertor project how we use this method to convert the text inside the TextBox to binary (and then back to string so that we can show it in the second TextBox):

txtDec2.Text = Convert.ToInt64(txtBin2.Text, 2).ToString();


Notice that we passed two parameters to the ToInt64() method. One is the actual number (retrieved from the TextBox), and one is the number 2 which represents the base (base 2, being the binary system).

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top