Convert Binary Number to Decimal Number in C#

Binary Number: A Number expressed using the base 2  with digits 0 and 1.

Decimal Number: A Number expressed using the base 10 with digits 0 to 9.

Conversion Example: Suppose we want to convert binary number 1011 to decimal. The followiing is the conversion process:

Step 1: 1* 2^0=1
Step 2: 1* 2^1=2
Step 3: 0*2^2=0
Step 4: 1*2^3=8

Its decimal value = 1 + 2 + 0 + 8 = 11

So (1011)2 = (11)10 

  1. class BinaryToDecimal {  
  2.     public int Power(int n) {  
  3.         if (n == 0) return 1;  
  4.         else return 2 * Power(n - 1);  
  5.   
  6.     }  
  7.     static void Main() {  
  8.         BinaryToDecimal bd = new BinaryToDecimal();  
  9.         Console.WriteLine("Enter any binary no");  
  10.         string s1 = Console.ReadLine();  
  11.         string s2 = "";  
  12.         for (int i = s1.Length - 1; i >= 0; i--) {  
  13.             s2 += s1[i];  
  14.         }  
  15.         int sum = 0;  
  16.         for (int i = 0; i < s2.Length; i++) {  
  17.             int res = bd.Power(i);  
  18.             sum = sum + (res * int.Parse(s2[i].ToString()));  
  19.         }  
  20.         Console.WriteLine(sum);  
  21.         Console.ReadLine();  
  22.     }  
  23. }