Advertisement
Guest User

Untitled

a guest
May 17th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4.  
  5. namespace _07.One_System_to_any_other
  6. {
  7.     class Program
  8.     {
  9.         static void Main()
  10.         {
  11.             int sBase = int.Parse(Console.ReadLine());
  12.             string n = Console.ReadLine().ToUpper();
  13.             int dBase = int.Parse(Console.ReadLine());
  14.  
  15.             //turn n in sBase into n in base 10
  16.             BigInteger toDecimal = IntoDecimal(sBase, n);
  17.             //turn n in base 10 into n in base 9
  18.             string result = DecimalTodBase(dBase, toDecimal);
  19.             Console.WriteLine(result);
  20.  
  21.             //Console.WriteLine(DecimalTodBase(13,207));
  22.  
  23.  
  24.         }
  25.         static BigInteger IntoDecimal(int inBase, string n)
  26.         {
  27.             BigInteger result = 0;
  28.             BigInteger multiplier;
  29.             //for every digit in the number, startng from the left
  30.             //multiplying each digit by base^(place of the digit from right to left)
  31.             for (int i = n.Length - 1; i >= 0; i--)
  32.             {
  33.                 multiplier = 1;
  34.                 //manual .Pow
  35.                 for (int j = 0; j < i; j++)
  36.                 {
  37.                     multiplier *= inBase;
  38.                 }
  39.  
  40.                 //if the digit is a numer (1-9)
  41.                 if (n[n.Length - 1 - i] < 57)
  42.                 {
  43.                     result += (BigInteger)(n[n.Length - 1 - i] - '0') * multiplier;
  44.                 }
  45.                 else //if nto a numebr - ABCDEF
  46.                 {
  47.                     result += (n[n.Length - 1 - i] - 55) * multiplier;
  48.                 }
  49.             }
  50.             return result;
  51.         }
  52.  
  53.         static string DecimalTodBase(int outBase, BigInteger n)
  54.         {
  55.             BigInteger multiplier = 1;
  56.             string result = "";
  57.             BigInteger timesMet;
  58.  
  59.             //finding largest n to the power, i.e. the biggest digit of the new number
  60.             while (n / multiplier > 0)
  61.             {
  62.                 multiplier *= outBase;
  63.             }
  64.             multiplier /= outBase;
  65.  
  66.             do //for every digit (example: 9^3, 9^2, 9^1, 9^0) getting the "timesmet" (example: 8 * 9^3)
  67.             {
  68.                 timesMet = n / multiplier;
  69.                 n = n - (timesMet * multiplier);
  70.                 if (timesMet < 10) //if its a real number
  71.                 {
  72.                     result = result + timesMet;
  73.                 }
  74.                 else //if not, asign it with a letter (ABCDEF...)
  75.                 {
  76.                     result = result + (char)(55 + timesMet);
  77.                  
  78.                 }
  79.                 multiplier /= outBase;
  80.             }
  81.             while (multiplier >= 1); //while its multiplier is bigger or equal to (example 9^0)
  82.  
  83.             return result;
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement