Advertisement
Martichka

[C#] Numeral Systems/ Task - AllTypes

Jan 24th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System;
  2. class AnyOtherNumSys
  3. {
  4.     //Binary to decimal
  5.     static void BinToDec()
  6.     {
  7.         Console.WriteLine("Enter your binary number:");
  8.         string value = Console.ReadLine();
  9.         int num = Convert.ToInt32(value, 2);
  10.         Console.WriteLine("Binary {0} converted to dec {1}.", value, num.ToString());
  11.     }
  12.  
  13.     //Binary to hexadecimal
  14.     static void BinToHex()
  15.     {
  16.         Console.WriteLine("Enter your binary number:");
  17.         string num = Console.ReadLine();
  18.         int decNum = Convert.ToInt32(num, 2);
  19.         string decInHex = decNum.ToString("x");
  20.         Console.WriteLine("Hexadecimal representation: {0}", decInHex);
  21.     }
  22.     //hexadecimal to decimal
  23.     static void HexToDec()
  24.     {
  25.         Console.WriteLine("Enter your hexadecimal number:");
  26.         string value = Console.ReadLine();
  27.         int num = Convert.ToInt32(value, 16);
  28.         Console.WriteLine("Hex 0x{0} converted to dec {1}.", value, num.ToString());
  29.     }
  30.     //hexadecimal to binary
  31.     static void HexToBin()
  32.     {
  33.         Console.WriteLine("Enter your hexadecimal number:");
  34.         string temp = Console.ReadLine();
  35.         int num = Convert.ToInt32(temp, 16);
  36.         string dec = Convert.ToString(num, 2);
  37.         Console.WriteLine("Binary representation {0} ", dec);
  38.     }
  39.     //decimal to binary
  40.     static void DecToBin()
  41.     {
  42.         Console.WriteLine("Enter your decimal number:");
  43.         int dec = int.Parse(Console.ReadLine());
  44.         Console.WriteLine("Binary representation {0} ",Convert.ToString(dec, 2));
  45.     }
  46.     //decimal to hexadecimal
  47.     static void DecToHex()
  48.     {
  49.         Console.WriteLine("Enter your decimal number:");
  50.         int dec = int.Parse(Console.ReadLine());
  51.         string decInHex = dec.ToString("x");
  52.         Console.WriteLine("Hexadecimal representation "+ decInHex);
  53.     }
  54.     //choises
  55.     static void Choise(string choise, string changeTo)
  56.     {
  57.         switch (choise)
  58.         {
  59.             case "1":
  60.                 if (changeTo == "3")
  61.                 {
  62.  
  63.                     Console.WriteLine("Convert to decimal");
  64.                     BinToDec();
  65.                 }
  66.                 if (changeTo == "2")
  67.                 {
  68.                     Console.WriteLine("Convert to hexadecimal");
  69.                     BinToHex();
  70.                 }
  71.                 break;
  72.             case "2":
  73.                 if (changeTo == "3")
  74.                 {
  75.                     Console.WriteLine("Convert to decimal");
  76.                     HexToDec();
  77.                 }
  78.                 if (changeTo == "1")
  79.                 {
  80.                     Console.WriteLine("Convert to binary");
  81.                     HexToBin();
  82.                 }
  83.                 break;
  84.             case "3":
  85.                 if (changeTo == "1")
  86.                 {
  87.                     Console.WriteLine("Convert to binary");
  88.                     DecToBin();
  89.                 }
  90.                 if (changeTo == "2")
  91.                 {
  92.                     Console.WriteLine("Convert to hexadecimal");
  93.                     DecToHex();
  94.                 }
  95.                 break;
  96.             default:
  97.                 Console.WriteLine("Wrong Choise!");
  98.                 Main();
  99.                 break;
  100.         }
  101.     }
  102.     static void Main()
  103.     {
  104.         Console.WriteLine("Choose a numeral system:\r\n1.Binary\r\n2. Hexadecimal\r\n3. Decimal");
  105.         string makeChoise = Console.ReadLine();
  106.         Console.Write("Convert to: ");
  107.         string changeTo = Console.ReadLine();
  108.         Choise(makeChoise, changeTo);
  109.         Console.WriteLine();
  110.         Main();
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement