Advertisement
sashomaga

Convert binary to decimal

Jan 18th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.54 KB | None | 0 0
  1. using System;
  2. //Write a program to convert binary numbers to their decimal representation
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         Console.WriteLine("Enter binary number : ");
  8.         string convert = Console.ReadLine();
  9.  
  10.         int result = 0;
  11.         for (int i = convert.Length - 1, j = 0; i >= 0; i--, j++)
  12.         {
  13.  
  14.             if (convert[i] == '1')
  15.             {
  16.                 result ^= 1 << j;
  17.             }
  18.         }
  19.         Console.WriteLine("Converted number :");
  20.         Console.WriteLine(result);
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement