Advertisement
wingman007

AlgorithmsDataStructuresConvert2BinaryAndBack

Oct 10th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. // Angel Georgiev <georgiev1997@gmail.com>
  2. using System;
  3. using System.Text;
  4. using System.Collections.Generic;
  5.  
  6. namespace Parser
  7. {
  8.     class Program
  9.     {
  10.         static string input;
  11.  
  12.         static void Main(string[] args)
  13.         {
  14.             Console.WriteLine("Welcome! Input a number to convert it to binary. Type Q to quit. ");
  15.  
  16.             do
  17.             {
  18.                 Console.WriteLine("What number should we convert?");
  19.                 input = Console.ReadLine();
  20.  
  21.                 string number = input;
  22.                 number = number.Trim();
  23.  
  24.                 Console.WriteLine("What system?");
  25.                 input = Console.ReadLine();
  26.  
  27.                 int system = 0;
  28.                 if (int.TryParse(input, out system))
  29.                 {
  30.                     if (Validate(number, system))
  31.                     {
  32.                         Console.WriteLine("The converted number is : " +    ToDecimal(number, system));
  33.                         continue;
  34.                     }
  35.                 }
  36.                 Console.WriteLine("Invalid input!");
  37.             } while (input != "q");
  38.         }
  39.  
  40.         static bool Validate(string number, int systemBase)
  41.         {
  42.             for (int i = 0; i < number.Length; i++)
  43.             {
  44.                 int cast;
  45.                 if (int.TryParse(number[i].ToString(), out cast))
  46.                 {
  47.                     if (cast >= systemBase)
  48.                     {
  49.                         return false;
  50.                     }
  51.                 }
  52.                 else
  53.                 {
  54.                     return false;
  55.                 }
  56.             }
  57.             return true;
  58.         }
  59.  
  60.         static string ToDecimal(string number, int systemBase)
  61.         {
  62.  
  63.             int result = 0;
  64.  
  65.             for (int i = 0; i < number.Length; i++)
  66.             {
  67.                 int element = int.Parse(number[number.Length - i - 1].ToString());
  68.  
  69.                 for (int j = 0; j < i; j++)
  70.                 {
  71.                     element *= systemBase;
  72.                 }
  73.                 result += element;
  74.             }
  75.  
  76.             return result.ToString();
  77.         }
  78.  
  79.         static string FromDecimal(int number, int systemBase)
  80.         {
  81.             List<char> result = new List<char>();
  82.  
  83.             do
  84.             {
  85.                 result.Insert(0, (number % systemBase).ToString()[0]);
  86.                 number /= systemBase;
  87.  
  88.             } while (number != 0);
  89.  
  90.             return CollectionToString(result);
  91.         }
  92.  
  93.         static string CollectionToString(IReadOnlyList<char> list)
  94.         {
  95.             StringBuilder text = new StringBuilder();
  96.             foreach (char symbol in list)
  97.             {
  98.                 text.Append(symbol);
  99.             }
  100.  
  101.             return list.ToString();
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement