C# Binary to Decimal Conversion

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is Binary to Decimal Conversion?

Binary (base 2) uses only bits 0 and 1. Decimal (base 10) is the everyday number system. Conversion means reading each bit as a power of two and summing the contributions of every 1.

Remember
Rightmost bit = 2⁰ (value 1)
Next bit       = 2¹ (value 2)
Next bit       = 2² (value 4)
…

Example: 101010
  1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1 = 42
BinaryPlace values usedDecimal
10108 + 210
11118 + 4 + 2 + 115
10101032 + 8 + 242

How to Convert Binary to Decimal in C#

Four clear steps — then pick a manual loop or the built-in parser.

  1. Read the binary string (only 0 and 1 allowed).
  2. Validate every character before converting.
  3. Sum place values (right-to-left), or call Convert.ToInt32(s, 2).
  4. Print or return the decimal result.
MethodIdeaTimeBest for
Place-value loopWalk right-to-left; add 2^power for each 1O(k)Interviews, learning place values
Convert.ToInt32(s, 2)Parse the string as base 2 in one callO(k)Production code, short answers

Pseudocode

Pseudocode
function BinaryToDecimal(bits):
    if bits is empty:
        reject input

    decimal = 0
    power = 1                          // 2⁰

    for i from length(bits) - 1 down to 0:
        ch = bits[i]
        if ch is not '0' and not '1':
            reject input
        if ch == '1':
            decimal = decimal + power
        power = power * 2

    return decimal

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Valid bitsOnly 0 and 1
Place values1, 2, 4, 8, 16, 32, 64, 128, …
Start sideRightmost bit = 2⁰
Built-inConvert.ToInt32(bits, 2)
Longer stringsConvert.ToInt64(bits, 2)
Classic check101010 → 42

Live Preview

Enter a binary string to see place values and the decimal result.

Digits 0 and 1 only (up to 31 bits). Tap a chip or type a value.

Result
Binary  = 101010
Places  = 32 + 8 + 2
Decimal = 42

Worked Walkthrough — 101010

Start at the right. Multiply each bit by its place value, then add the non-zero terms.

Calculation
Bit:     1   0   1   0   1   0
Power:   5   4   3   2   1   0
Value:  32  16   8   4   2   1

1×32 + 0×16 + 1×8 + 0×4 + 1×2 + 0×1
= 32 + 8 + 2
= 42

C# Programs

Start with the place-value loop so you can explain every step. Then use Convert.ToInt32 for a one-line production-style answer.

Example 1 — Place-Value Loop

Walk the string from right to left. Double the place value after each bit.

C#
using System;

class Program
{
    static int BinaryToDecimal(string bits)
    {
        if (string.IsNullOrEmpty(bits))
        {
            throw new ArgumentException("Binary string cannot be empty.");
        }

        int decimalValue = 0;
        int power = 1;

        for (int i = bits.Length - 1; i >= 0; i--)
        {
            char ch = bits[i];
            if (ch != '0' && ch != '1')
            {
                throw new ArgumentException("Only digits 0 and 1 are allowed.");
            }

            if (ch == '1')
            {
                decimalValue += power;
            }

            power *= 2;
        }

        return decimalValue;
    }

    static void Main()
    {
        string binary = "101010";
        Console.WriteLine(binary + " → " + BinaryToDecimal(binary));
    }
}

How It Works

  1. Reject an empty string before any math begins.
  2. Start at the rightmost bit with power = 1 (that is 20).
  3. For each character, accept only '0' or '1'; anything else throws.
  4. If the bit is '1', add the current power to decimalValue. A '0' adds nothing.
  5. Double power (*= 2) and move one position left. Repeat until the first character.
Trace — 101010
bit 0 → power 1  → skip (0)
bit 1 → power 2  → add 2
bit 0 → power 4  → skip
bit 1 → power 8  → add 8
bit 0 → power 16 → skip
bit 1 → power 32 → add 32

total = 2 + 8 + 32 = 42

Example 2 — Convert.ToInt32

Validate the bits, then let the framework parse the string as base 2.

C#
using System;

class Program
{
    static int BinaryToDecimal(string bits)
    {
        if (string.IsNullOrEmpty(bits))
        {
            throw new ArgumentException("Binary string cannot be empty.");
        }

        foreach (char ch in bits)
        {
            if (ch != '0' && ch != '1')
            {
                throw new ArgumentException("Only digits 0 and 1 are allowed.");
            }
        }

        return Convert.ToInt32(bits, 2);
    }

    static void Main()
    {
        Console.WriteLine(BinaryToDecimal("101010")); // 42
        Console.WriteLine(BinaryToDecimal("1111"));   // 15
    }
}

How It Works

  1. Reject an empty or null string immediately.
  2. Scan every character once. If any digit is not 0 or 1, throw — this stops a confusing FormatException later.
  3. Call Convert.ToInt32(bits, 2). The second argument means “treat this string as base 2.”
  4. The method returns a normal decimal int (for example "101010" → 42, "1111" → 15).

Use this after you understand place values. Prefer the manual loop in interviews when the interviewer wants to see the math; prefer Convert.ToInt32 in real projects for clarity and fewer bugs.

Edge Cases & Pitfalls

Check these before calling the solution done.

Empty

Reject empty input

Throw or return a clear error — never convert silently.

0 / 1 only

Validate every character

2, spaces, or letters cause FormatException in Convert.ToInt32.

Leading 0

Still valid

00101 equals 5. Leading zeros do not change the value.

Overflow

Watch int / long limits

Use ToInt64 or BigInteger for strings longer than 31 / 63 bits.

Time and Space Complexity

ProgramTimeExtra space
Place-value loopO(k)O(1)
Convert.ToInt32(s, 2)O(k)O(1)

k is the number of bits in the input string.

Key Takeaways

  • Place values: rightmost bit is 2⁰; only 1-bits add to the total.
  • Validate first: reject empty strings and any character outside 0/1.
  • Two options: manual place-value loop, or Convert.ToInt32(s, 2).
  • Test set: 101010 → 42; 1111 → 15; 0 → 0.

One line: sum powers of two for each 1-bit — or call Convert.ToInt32(bits, 2).

Frequently Asked Questions

Sum powers of two for each 1-bit, or call Convert.ToInt32(binaryString, 2). Both turn a base-2 string into a base-10 integer.
It parses string s as a base-2 number and returns its decimal int value. Use Convert.ToInt64 for longer bit strings.
Binary may contain only 0 and 1. Validation avoids FormatException and wrong answers from invalid characters.
No. Strings like 00101 are valid and equal 5 in decimal.
Both the manual loop and Convert.ToInt32 are O(k), where k is the number of bits. Extra space is O(1) beyond the input.

Next: Common Divisors

Find every positive integer that divides two numbers evenly.

Common divisors tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful