Advertisement
Guest User

From Any To Any Numeral System

a guest
Jun 21st, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. public class ConvertFromAnyNumSystem
  5. {
  6.     static void Main()
  7.     {
  8.         // user input
  9.         Console.Write("Enter a number: ");
  10.         string number = (Console.ReadLine()).ToUpper();
  11.         Console.Write("Enter numberal system to convert From: ");
  12.         int s = int.Parse(Console.ReadLine());
  13.         Console.Write("Enter numberal system to convert To: ");
  14.         int d = int.Parse(Console.ReadLine());
  15.  
  16.         // validation - input numbers are to be in the required range: (2 ≤ s, d ≤ 16)
  17.         if (s < 2 || d < 2 || d > 16 || s > 16)
  18.         {
  19.             Console.WriteLine("Wrong input!Number should be in range [2 to 16]");
  20.         }
  21.         else
  22.         {
  23.             // applying the ConvertToDec(number, s) method on the input numbers,
  24.             // and applying the DecimalToBase() method on the result
  25.             Console.WriteLine(DecimalToBase(ConvertToDec(number, s), d));
  26.         }
  27.     }
  28.  
  29.     // converting all input numbers to decimal numeral system first
  30.     public static int ConvertToDec(string number, int baseFrom)
  31.     {
  32.         int decNum = 0;
  33.         for (int i = 0; i < number.Length; i++)
  34.         {
  35.             if (number[i] > '9') // if a char digit is > '9', it can only be 'A', 'B', etc.
  36.             //from the hexadecimal numeral system
  37.             {
  38.                 decNum += (number[i] - '7') * (int)Math.Pow(baseFrom, (number.Length - 1 - i));
  39.                 // example: 'A' = 65 (ASCII code); '7' = 55; hence 'A' - '7' = 10,
  40.                 // which is exactly the decimal representation of the digit A
  41.             }
  42.             else
  43.             {
  44.                 decNum += (number[i] - '0') * (int)Math.Pow(baseFrom, (number.Length - 1 - i));
  45.                 // example: '9' = 57 (ASCII code); '0' = 48; hence '9' - '0' = 9,
  46.                 // which is exactly the decimal value of the digit 9
  47.             }
  48.         }
  49.         return decNum;
  50.     }
  51.  
  52.     public static string DecimalToBase(long decimalNumber, int numeralSystem)
  53.     {
  54.         string result = "";
  55.         while (decimalNumber > 0)
  56.         {
  57.             // calculating remainder, after the input number
  58.             // is divided by the number representing the numeral system
  59.             // and we obtain the input number's digits
  60.             long digit = decimalNumber % numeralSystem;
  61.  
  62.             if (digit >= 0 && digit <= 9)
  63.             // example: '9' = 57 (ASCII code); '0' = 48; hence 9 - '0' = 9 + 48 = 57,
  64.             // which is exactly the ASCII code of the char '9'
  65.             // and the result is actually a string of chars, representing the separate digits of the number
  66.             {
  67.                 result += (char)(digit + '0'); // we add one more char to the string representation of the number
  68.             }
  69.  
  70.             // if a digit is > 9, it can only be A, B, etc.
  71.             //from the hexadecimal numeral system
  72.             else
  73.             {
  74.                 result += (char)(digit + '7');
  75.                 // example: 'A' = 65 (ASCII code); '7' = 55; hence A + '7' = 10 + 55 = 65,
  76.                 // which is exactly the ASCII code of the char 'A'
  77.             }
  78.  
  79.             // we divvide the decimal number by the number representing the numeral system,
  80.             // so that we can re-apply the code below, and keep extracting digits,
  81.             // until the decimal number has no more digits
  82.             // and all its digits have been converted to chars,
  83.             // and added to the string representation of the result number
  84.             decimalNumber /= numeralSystem;
  85.         }
  86.         var reversed = result.Reverse(); // we will extract the digits in reversed order, so we need to reverse the order back to the correct one
  87.         string output = string.Join("", reversed);
  88.         return output;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement