Advertisement
sashomaga

Convert

Jan 18th, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. //Write a program to convert from any numeral system of given
  4. //base s to any other numeral system of base d (2 ≤ s, d ≤  16).
  5. class AnyNumeralSystem
  6. {
  7.     static void Main()
  8.     {
  9.         Console.WriteLine("Enter number: ");
  10.         string convert = Console.ReadLine();
  11.         Console.WriteLine("Enter bace of numeral system: ");
  12.         int bace = int.Parse(Console.ReadLine());
  13.         Console.WriteLine("Enter new bace of numeral system: ");
  14.         int newBace = int.Parse(Console.ReadLine());
  15.  
  16.         //convert to decimal system
  17.         int decimalNum = 0;
  18.         for (int i = convert.Length-1, reverse = 0; i>= 0; i--, reverse++)
  19.         {
  20.             decimalNum += (convert[i]-'0') * (int)Math.Pow(bace, reverse);
  21.         }
  22.         //convert to new system
  23.         StringBuilder builder = new StringBuilder();
  24.         while (decimalNum > 0)
  25.         {
  26.             builder.Insert(0, decimalNum % newBace);
  27.             decimalNum /= newBace;
  28.         }
  29.         Console.WriteLine("Converted number: \n{0}",builder.ToString());
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement