Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Convert2Base
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = 255;
- // n = 0xaab;
- for (byte b = 2; b < 35; b++)
- {
- Console.WriteLine();
- Console.WriteLine("{0:D}", n);
- Console.WriteLine(Convert.ToString(n, 10));
- Console.WriteLine(Convert.ToString(n, 2));
- Console.WriteLine(Convert.ToString(n, 8));
- Console.WriteLine("{0:X}", n);
- Console.WriteLine(Convert.ToString(n, 16));
- Console.WriteLine("Base {0}:", b);
- Console.WriteLine(Convert2Base(n, b));
- Console.WriteLine(Convert2Decimal(Convert2Base(n, b), b));
- }
- }
- static string Convert2Base(int n, byte b)
- {
- if (b > 36) throw new Exception("Can not convert to base bigger than 36");
- string result = "";
- string[] s = new string[128];
- int reminder = 0;
- int i = 0;
- while (n >= b)
- {
- reminder = n % b;
- if (reminder > 9)
- {
- s[i] = ConvertNuber2Char(reminder).ToString();
- }
- else
- {
- s[i] = Convert.ToString(reminder);
- }
- n /= b;
- i++;
- }
- if (n > 9)
- {
- s[i] = ConvertNuber2Char(n).ToString();
- }
- else
- {
- s[i] = Convert.ToString(n);
- }
- for (; i >= 0; i--)
- {
- result += s[i];
- }
- return result;
- }
- static char ConvertNuber2Char(int n)
- {
- // ToDo use UTF-8 code to convert not a switch
- if (n > 35) throw new Exception("Can not convert number bigger than 35 to characters from some alphabet");
- char c = '#';
- switch (n)
- {
- case 10:
- c = 'A';
- break;
- case 11:
- c = 'B';
- break;
- case 12:
- c = 'C';
- break;
- case 13:
- c = 'D';
- break;
- case 14:
- c = 'E';
- break;
- case 15:
- c = 'F';
- break;
- case 16:
- c = 'G';
- break;
- case 17:
- c = 'H';
- break;
- case 18:
- c = 'I';
- break;
- case 19:
- c = 'J';
- break;
- case 20:
- c = 'K';
- break;
- case 21:
- c = 'L';
- break;
- case 22:
- c = 'M';
- break;
- case 23:
- c = 'N';
- break;
- case 24:
- c = 'O';
- break;
- case 25:
- c = 'P';
- break;
- case 26:
- c = 'Q';
- break;
- case 27:
- c = 'R';
- break;
- case 28:
- c = 'S';
- break;
- case 29:
- c = 'T';
- break;
- case 30:
- c = 'U';
- break;
- case 31:
- c = 'V';
- break;
- case 32:
- c = 'W';
- break;
- case 33:
- c = 'X';
- break;
- case 34:
- c = 'Y';
- break;
- case 35:
- c = 'Z';
- break;
- case 36:
- c = 'Б'; // :)
- break;
- }
- return c;
- }
- static int Convert2Decimal(string n, byte b)
- {
- int result = 0;
- int number = 0;
- for (int i = 0; i < n.Length; i++)
- {
- try
- {
- number = int.Parse(n[i].ToString());
- }
- catch
- {
- number = ConvertChar2Number(n[i]);
- }
- result += number * (int)Math.Pow(b, n.Length - 1 - i);
- }
- return result;
- }
- static int ConvertChar2Number(char c)
- {
- // ToDo use UTF-8 code to convert not a switch
- // if (n > 35) throw new Exception("Can not convert number bigger than 35 to characters from some alphabet");
- int n = 0;
- c = c.ToString().ToUpper()[0]; // convert the char to uppercase
- switch (c)
- {
- case 'A':
- n = 10;
- break;
- case 'B':
- n = 11;
- break;
- case 'C':
- n = 12;
- break;
- case 'D':
- n = 13;
- break;
- case 'E':
- n = 14;
- break;
- case 'F':
- n = 15;
- break;
- case 'G':
- n = 16;
- break;
- case 'H':
- n = 17;
- break;
- case 'I':
- n = 18;
- break;
- case 'J':
- n = 19;
- break;
- case 'K':
- n = 20;
- break;
- case 'L':
- n = 21;
- break;
- case 'M':
- n = 22;
- break;
- case 'N':
- n = 23;
- break;
- case 'O':
- n = 24;
- break;
- case 'P':
- n = 25;
- break;
- case 'Q':
- n = 26;
- break;
- case 'R':
- n = 27;
- break;
- case 'S':
- n = 28;
- break;
- case 'T':
- n = 29;
- break;
- case 'U':
- n = 30;
- break;
- case 'V':
- n = 31;
- break;
- case 'W':
- n = 32;
- break;
- case 'X':
- n = 33;
- break;
- case 'Y':
- n = 34;
- break;
- case 'Z':
- n = 35;
- break;
- case 'Б':
- n = 36; // :)
- break;
- default:
- throw new Exception("The char can not be converted to a number");
- }
- return n;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement