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