C# Program to Convert Binary to Decimal with Examples

Introduction:

In this article, we’ll explore how to write a C# Program to Convert Binary to Decimal.

Section 1: Basics of Binary and Decimal Systems

1.1 Binary System

Before diving into C# code, let’s grasp the basics. The binary system uses only two digits, 0 and 1. Each digit is called a bit, and the position of each bit represents a power of 2.

1.2 Decimal System

The decimal system uses base 10 and includes digits from 0 to 9. Each position in a decimal number represents a power of 10.

Section 2: C# Program to Convert Binary to Decimal

Now, let’s translate the theory into C# code.

2.1 Using Convert.ToInt32() Method

Following is the C# Program to Convert Binary to Decimal Using Convert.ToInt32() method.

using System;

class BinaryToDecimalConverter
{
    static void Main()
    {
        string binaryNumber = "101011";
        int decimalNumber = Convert.ToInt32(binaryNumber, 2);

        Console.WriteLine($"Binary: {binaryNumber}\nDecimal: {decimalNumber}");
    }
}

Output:

Binary: 101011
Decimal: 43

Code Explanation:

  • We use Convert.ToInt32() method to convert the binary string to an integer.
  • The second parameter, 2, indicates that the input is in base 2 (binary).

2.2 Manual Conversion Using Loops

using System;

class BinaryToDecimalConverter
{
    static void Main()
    {
        string binaryNumber = "101011";
        int decimalNumber = 0;

        for (int i = 0; i < binaryNumber.Length; i++)
        {
            int bit = int.Parse(binaryNumber[i].ToString());
            int power = binaryNumber.Length - 1 - i;
            decimalNumber += bit * (int)Math.Pow(2, power);
        }

        Console.WriteLine($"Binary: {binaryNumber}\nDecimal: {decimalNumber}");
    }
}

Output:

Binary: 101011
Decimal: 43

Code Explanation:

  • In the above code, the binaryNumber variable holds the binary input as a string.
  • The decimalNumber variable is initialized to store the result of the conversion.
  • We are using for loop to iterate through each bit of the binary number from left to right.
  • Inside the loop, bit is the current binary digit (0 or 1).
  • The power variable calculates the position value of the current bit.
  • The decimalNumber is updated by adding the contribution of the current bit using the formula bit ×2 power.
  • Finally, the original binary number and the resulting decimal number are printed to the console.

2.3 Performs binary-to-decimal conversion

The following C# program takes user input and performs binary-to-decimal conversion.

using System;

class BinaryToDecimalConverter
{
    static void Main()
    {
        // Infinite loop to keep the program running
        while (true)
        {
            // Prompt user for input
            Console.Write("Enter a binary number (or 'exit' to end): ");
            string userInput = Console.ReadLine();

            // Check if the user wants to exit
            if (userInput.ToLower() == "exit")
            {
                break; // Exit the loop if the user enters 'exit'
            }

            // Perform binary to decimal conversion
            try
            {
                int decimalNumber = Convert.ToInt32(userInput, 2);
                Console.WriteLine($"Binary: {userInput}\nDecimal: {decimalNumber}");
            }
            catch (FormatException)
            {
                Console.WriteLine("Invalid binary input. Please enter a valid binary number.");
            }
            catch (OverflowException)
            {
                Console.WriteLine("The entered binary number is too large to be represented as an integer.");
            }

            // Provide some space before the next iteration
            Console.WriteLine();
        }

        // Inform the user that the program is exiting
        Console.WriteLine("Program exiting. Thank you for using the Binary to Decimal Converter!");
    }
}

Output:

Enter a binary number (or 'exit' to end): 111111
Binary: 111111
Decimal: 63
Enter a binary number (or 'exit' to end): 101010
Binary: 101010
Decimal: 42

Enter a binary number (or 'exit' to end): exit
Program exiting. Thank you for using the Binary to Decimal Converter!

Recommended Articles:

Shekh Ali
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments