Advertisement
remote87

Binary string to decimal

Sep 8th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _13.BinaryToDecimal
  8. {
  9.     class BinaryToDecimal
  10.     {
  11.         static void Main()
  12.         {
  13.             Console.Write("Enter a binary numbes as string: ");
  14.             string binary = Console.ReadLine();
  15.             int digit = 0;
  16.             int power = 0;
  17.             long sum = 0L;
  18.             for (int i = binary.Length - 1; i >= 0; i--)
  19.             {
  20.                 digit = binary[i] - '0';
  21.                 double temp = Math.Pow(2, power);
  22.                 sum += (int)temp * digit;
  23.                 power++;
  24.             }
  25.             Console.WriteLine(sum);
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement