Created
August 28, 2013 21:00
-
-
Save bobixaka/6371212 to your computer and use it in GitHub Desktop.
Convert number from any numeral system to any other with base between 2 and 16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Write a program to convert from any numeral system of given base s to any other numeral system of base d (2 ≤ s, d ≤ 16). | |
using System; | |
using System.Collections.Generic; | |
using System.Numerics; | |
namespace Task_7_AnySystemConverter | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
Console.Write("Please enter INPUT base(2-16): "); | |
string input = Console.ReadLine(); | |
int InputBase = 0; | |
int OutputBase = 0; | |
double Decimal = 0; | |
string OutputReversed = ""; | |
string Output = ""; | |
while (int.TryParse(input, out InputBase) == false || InputBase < 2 || InputBase > 16) | |
{ | |
Console.Write("Please enter a valid base from 2 to 16: "); | |
input = Console.ReadLine(); | |
} | |
Console.Write("Please enter OUTPUT base(2-16): "); | |
input = Console.ReadLine(); | |
while (int.TryParse(input, out OutputBase) == false || OutputBase < 2 || OutputBase > 16) | |
{ | |
Console.Write("Please enter a valid base from 2 to 16: "); | |
input = Console.ReadLine(); | |
} | |
Console.Write("Please enter {0} numeral system number: ",InputBase); | |
input = Console.ReadLine(); | |
for (int i = 0; i < input.Length; i++) | |
{ | |
switch (input[i]) | |
{ | |
case '0': Decimal += 0; | |
break; | |
case 'x': Decimal += 0; | |
break; | |
case '1': Decimal += Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case '2': Decimal += 2 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case '3': Decimal += 3 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case '4': Decimal += 4 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case '5': Decimal += 5 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case '6': Decimal += 6 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case '7': Decimal += 7 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case '8': Decimal += 8 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case '9': Decimal += 9 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case 'A': Decimal += 10 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case 'B': Decimal += 11 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case 'C': Decimal += 12 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case 'D': Decimal += 13 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case 'E': Decimal += 14 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
case 'F': Decimal += 15 * Math.Pow(InputBase, input.Length - 1 - i); | |
break; | |
default: Console.WriteLine("ERROR!"); | |
break; | |
} | |
} | |
Decimal = Convert.ToInt32(Decimal); | |
while ((int)Decimal > 0) | |
{ | |
int Remainder = (int)Decimal % OutputBase; | |
Decimal /= OutputBase; | |
switch (Remainder) | |
{ | |
case 0: OutputReversed += 0; | |
break; | |
case 1: OutputReversed += 1; | |
break; | |
case 2: OutputReversed += 2; | |
break; | |
case 3: OutputReversed += 3; | |
break; | |
case 4: OutputReversed += 4; | |
break; | |
case 5: OutputReversed += 5; | |
break; | |
case 6: OutputReversed += 6; | |
break; | |
case 7: OutputReversed += 7; | |
break; | |
case 8: OutputReversed += 8; | |
break; | |
case 9: OutputReversed += 9; | |
break; | |
case 10: OutputReversed += "A"; | |
break; | |
case 11: OutputReversed += "B"; | |
break; | |
case 12: OutputReversed += "C"; | |
break; | |
case 13: OutputReversed += "D"; | |
break; | |
case 14: OutputReversed += "E"; | |
break; | |
case 15: OutputReversed += "F"; | |
break; | |
default: Console.WriteLine("ERROR!"); | |
break; | |
} | |
} | |
for (int i = OutputReversed.Length - 1; i >= 0; i--) | |
{ | |
Output += Convert.ToChar(OutputReversed[i]); | |
} | |
Console.WriteLine("\nOUTPU: {0}\n", Output); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment