Advertisement
dimipan80

6.17Loop_ConvertBinaryNumberToDecimalSystem

Mar 25th, 2014
954
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1. using System;
  2.  
  3. class ConvertBinaryToDecimalNumber
  4. {
  5.     static void Main ()
  6.     {
  7.         Console.WriteLine("Enter your Binary number, wich consist of '0' and '1':");
  8.         string binaryStr = Console.ReadLine();
  9.         byte [] binarics = new byte [binaryStr.Length];
  10.         for (int i = 0; i < binaryStr.Length; i++)
  11.         {
  12.             binarics [i] = byte.Parse(Convert.ToString((binaryStr [i])));
  13.         }
  14.         Array.Reverse(binarics);
  15.  
  16.         long numDecimal = 0;
  17.         long multiplier = 1;
  18.         foreach (var digit in binarics)
  19.         {
  20.             numDecimal += (digit * multiplier);
  21.             multiplier *= 2;
  22.         }
  23.  
  24.         Console.WriteLine("The Binary number in Decimal system looks like DECIMAL = {0} !",
  25.             numDecimal);
  26.         Console.ReadLine();
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement