How to implement the possibility of converting a number from the 10th SS to any SS up to the 8th

Help me write code to translate the number system from the 10th number system to any other number system up to the 8th. I wrote a similar code:

int number = Convert.ToInt32(textBox1.Text), system = Convert.ToInt32(textBox2.Text);
        string s = null;
        s = number % system + s;
        number /= system;
        label1.Text = string.Format(("{0}, = {1}"), system, s);

But, the performance of it is not present.

Author: MSDN.WhiteKnight, 2017-12-02

2 answers

int num = 10;
int sys = 2;

List<int> result = new List<int>();

while (num > 0)
{
    result.Add(num % sys);
    num /= sys;
}

result.Reverse();
Console.WriteLine("({0:d})_{1}", sys, string.Join("", result));
Console.ReadKey();
 1
Author: slippyk, 2017-12-04 06:29:21

Operability is not present -- you do a little wrong=) for example, it was worth the number to lead to the String type when writing to it

S = Convert.ToString( (number%system) ) + S;

Yes, it's a good idea to implement in a loop: divide the original number entirely by the divisor=(base CC raised to a power equal to the number(0,1,2... N) of the currently calculated digit of the output value); write the remainder of the division to the digit of the output value, leave the integer result for the next iteration of the cycle; multiply the divisor by osn.SS (for trace.iterations); repeat.

Thus, calculate the output value by one character, starting from the end (from the lowest digits to the highest).

 -2
Author: Alias, 2017-12-04 05:41:58