0
0
CsharpProgramBeginner · 2 min read

C# Program to Convert Binary to Decimal Number

You can convert a binary string to decimal in C# using Convert.ToInt32(binaryString, 2), for example: int decimalValue = Convert.ToInt32("1010", 2); converts binary "1010" to decimal 10.
📋

Examples

Input1010
Output10
Input11111111
Output255
Input0
Output0
🧠

How to Think About It

To convert binary to decimal, read the binary number as a string. Each digit represents a power of 2, starting from the right with 2^0. Multiply each digit by its power of 2 and add all results to get the decimal number.
📐

Algorithm

1
Get the binary number as a string input.
2
Initialize a decimal result to zero.
3
For each digit in the binary string from right to left:
4
Multiply the digit (0 or 1) by 2 raised to the position index.
5
Add the result to the decimal result.
6
Return the decimal result.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        string binaryString = "1010";
        int decimalValue = Convert.ToInt32(binaryString, 2);
        Console.WriteLine(decimalValue);
    }
}
Output
10
🔍

Dry Run

Let's trace the binary string "1010" through the code.

1

Input binary string

binaryString = "1010"

2

Convert binary to decimal

decimalValue = Convert.ToInt32("1010", 2) which calculates 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 8 + 0 + 2 + 0 = 10

3

Print decimal value

Output: 10

Index (from right)DigitCalculationRunning Total
000 * 2^0 = 00
111 * 2^1 = 22
200 * 2^2 = 02
311 * 2^3 = 810
💡

Why This Works

Step 1: Using Convert.ToInt32

The method Convert.ToInt32(string, 2) converts a binary string directly to an integer by interpreting it as base 2.

Step 2: Binary to decimal logic

Each binary digit is multiplied by 2 raised to its position index, starting from zero on the right, then summed to get the decimal value.

Step 3: Output the result

The decimal integer is printed using Console.WriteLine, showing the converted number.

🔄

Alternative Approaches

Manual conversion using loop
csharp
using System;
class Program {
    static void Main() {
        string binary = "1010";
        int decimalValue = 0;
        int power = 1;
        for (int i = binary.Length - 1; i >= 0; i--) {
            if (binary[i] == '1') {
                decimalValue += power;
            }
            power *= 2;
        }
        Console.WriteLine(decimalValue);
    }
}
This method shows the step-by-step logic but is longer and less concise than using Convert.ToInt32.
Using LINQ Aggregate
csharp
using System;
using System.Linq;
class Program {
    static void Main() {
        string binary = "1010";
        int decimalValue = binary.Aggregate(0, (acc, c) => acc * 2 + (c - '0'));
        Console.WriteLine(decimalValue);
    }
}
This approach uses LINQ to process the string in a functional style, which is elegant but may be less clear for beginners.

Complexity: O(n) time, O(1) space

Time Complexity

The conversion processes each character of the binary string once, so it takes linear time relative to the string length.

Space Complexity

The method uses constant extra space, only storing a few variables regardless of input size.

Which Approach is Fastest?

Using Convert.ToInt32 is fastest and simplest; manual loops or LINQ add overhead and complexity.

ApproachTimeSpaceBest For
Convert.ToInt32O(n)O(1)Quick and simple conversion
Manual loopO(n)O(1)Learning and understanding logic
LINQ AggregateO(n)O(1)Functional style, concise code
💡
Use Convert.ToInt32(binaryString, 2) for a quick and reliable binary to decimal conversion in C#.
⚠️
Beginners often forget to specify base 2 in Convert.ToInt32, causing errors or wrong results.