-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRadixConverter.cs
139 lines (117 loc) · 3.65 KB
/
RadixConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Linq;
class RadixConverter{
static void Main(string[] args){
Console.ForegroundColor = ConsoleColor.Green;
Console.CursorVisible = false;
Console.Title = "Radix Converter";
Console.WriteLine("<-----------Radix Converter----------->");
Console.WriteLine("Created by Morasiu ([email protected])");
ChooseOption();
Console.ReadKey();
}
private static void ChooseOption() {
Console.WriteLine("Options:");
Console.WriteLine("1 - Convert from decimal");
Console.WriteLine("2 - Convert to decimal");
var isOptionCorrect = false;
do {
var key = Console.ReadKey().Key;
switch (key) {
case ConsoleKey.D1:
isOptionCorrect = true;
ConvertFromDecimalAndDisplayResult();
break;
case ConsoleKey.D2:
ConvertToDecimalAndDisplayResult();
isOptionCorrect = true;
break;
default:
Console.Write("\b");
continue;
}
} while (!isOptionCorrect);
}
private static void ConvertToDecimalAndDisplayResult() {
Console.Write("\bNumber: ");
var number = Console.ReadLine();
var radix = GetRadix();
var result = ConvertToDecimal(number, radix);
Console.WriteLine(number + " from radix " + radix + " to decimal is equal " + result);
}
private static void ConvertFromDecimalAndDisplayResult() {
var number = GetNumber();
var radix = GetRadix();
var result = ConvertFromDecimal(number, radix);
Console.WriteLine(number + " in " + radix + " is: " + result);
}
private static int GetNumber() {
Console.Write("\bNumber: ");
var input = Console.ReadLine();
var number = 0;
try {
number = Convert.ToInt32(input);
}
catch (FormatException) {
ExitWithError("ERROR: Wrong number");
}
return number;
}
private static int GetRadix() {
Console.Write("\bRadix: ");
var input = Console.ReadLine();
var radix = 0;
try {
radix = Convert.ToInt32(input);
}
catch (FormatException) {
ExitWithError("ERROR: Wrong number");
}
if (radix > 36 || radix < 2)
ExitWithError("ERROR: Radix needs to be between 2 and 36");
return radix;
}
private static string ConvertFromDecimal(int number, int radix) {
var result = "";
var inputNumber = number;
var alphabet = GetAlphabet();
while (inputNumber >= radix) {
var modal = inputNumber % radix;
if (modal >= 10)
result += alphabet[modal - 10];
else
result += modal;
inputNumber = inputNumber / radix;
}
if (inputNumber > 9)
result += alphabet[inputNumber - 10];
else
result += inputNumber;
return string.Concat(result.Reverse());
}
private static long ConvertToDecimal(string number, int radix) {
var result = new long();
var inputNumber = number.Reverse().ToList();
for (var index = 0; index < inputNumber.Count(); index++) {
result += GetNumberFromChar(inputNumber[index]) * (long) Math.Pow(radix, Convert.ToDouble(index));
}
return result;
}
private static char[] GetAlphabet() {
return Enumerable.Range('A', 26).Select(x => (char)x).ToArray();
}
private static int GetNumberFromChar(char number) {
try {
return Convert.ToInt32(number);
}
catch (FormatException) {}
var alphabet = GetAlphabet();
if (!alphabet.Contains(number.ToString().ToUpper()[0])) throw new ArgumentException("Wrong number");
var index = alphabet.ToList().FindIndex(c => c == number.ToString().ToUpper()[0]);
return index + 10;
}
private static void ExitWithError(string s) {
Console.WriteLine(s);
Environment.Exit(1);
}
}