Advertisement
Alekscho85

BinaryToDecimalNumber

Oct 26th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.02 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. namespace _13.BinaryToDecimalNumber
  4. {
  5.     class BinaryToDecimalNumber
  6.     {
  7.         static void Main()
  8.         {
  9.             Console.WriteLine("Enter binary number: ");
  10.             string nBinaryString = Console.ReadLine();
  11.             int[] nBinaryArray = new int[nBinaryString.Length];
  12.             for (int i = 0; i < nBinaryString.Length; i++)
  13.             {
  14.                 nBinaryArray[i] = Convert.ToInt32(Convert.ToString(nBinaryString[i]));
  15.             }
  16.             Array.Reverse(nBinaryArray);
  17.             double nDecimal = 0;
  18.             for (int j = 0; j < nBinaryString.Length; j++)
  19.             {
  20.                 if (nBinaryArray[j] == 1)
  21.                 {
  22.                     nDecimal += Math.Pow(2, j);
  23.                 }
  24.                 else
  25.                 {
  26.                     nDecimal = nDecimal + 0;
  27.                 }
  28.             }
  29.             long nDecimalLong = Convert.ToInt64(nDecimal);
  30.             Console.WriteLine(nDecimalLong);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement