Ad

Create a number converter which is converting integers numbers of 2 to 16 systems.
sourceS - source system of the convN.
targetS - convN will be converted to this system.
convN - convertible number.

using System;
public class NumberConverter
{
   static long sum1;
        public static string Converting(float sourceS, int targetS, string convN)
        {
            int l = convN.Length;
            char[] tx = convN.ToCharArray();
            decimal[] massive = new decimal[l];
            for (int ls = 0, i = l - 1; ls <l; i--, ls++)
            {
                switch (tx[ls])
                {
                    case 'A': massive[i] = 10; continue;
                    case 'B': massive[i] = 11; continue;
                    case 'C': massive[i] = 12; continue;
                    case 'D': massive[i] = 13; continue;
                    case 'E': massive[i] = 14; continue;
                    case 'F': massive[i] = 15; continue;
                }
                massive[i] = Convert.ToDecimal(tx[ls].ToString());
            }
            int lm = massive.Length;
            double sum = 0;
            if (sourceS != 10)
            {
                if (targetS == 10)
                {
                    for (int i = lm - 1; 0 <= i; i--)
                    {
                        float numi = Convert.ToSingle(i);
                        float nummassive = Convert.ToSingle(massive[i]);
                        sum += nummassive * Math.Pow(sourceS, numi);
                    }
                    return sum.ToString();
                }
                else
                {
                    for (int i = lm - 1; 0 <= i; i--)
                    {
                        float numi = Convert.ToSingle(i);
                        float nummassive = Convert.ToSingle(massive[i]);
                        sum += nummassive * Math.Pow(sourceS, numi);
                    }
                    sum1 = Convert.ToInt64(sum);
                }
            }
            else sum1 = Convert.ToInt64(convN);
            string numberResultBefore = null;
            while (sum1 >= targetS - 1)
            {
                switch (sum1 % targetS)
                {
                    case 10: if (targetS <= 10) break; numberResultBefore += "A"; break;
                    case 11: if (targetS <= 11) break; numberResultBefore += "B"; break;
                    case 12: if (targetS <= 12) break; numberResultBefore += "C"; break;
                    case 13: if (targetS <= 13) break; numberResultBefore += "D"; break;
                    case 14: if (targetS <= 14) break; numberResultBefore += "E"; break;
                    case 15: if (targetS <= 15) break; numberResultBefore += "F"; break;
                    default: numberResultBefore += sum1 % targetS; break;
                }
                sum1 /= targetS;
            }

            switch (sum1)
            {
                case 10: if (targetS <= 10) break; numberResultBefore += "A"; break;
                case 11: if (targetS <= 11) break; numberResultBefore += "B"; break;
                case 12: if (targetS <= 12) break; numberResultBefore += "C"; break;
                case 13: if (targetS <= 13) break; numberResultBefore += "D"; break;
                case 14: if (targetS <= 14) break; numberResultBefore += "E"; break;
                case 15: if (targetS <= 15) break; numberResultBefore += "F"; break;
                default: numberResultBefore += sum1; break;

            }
            int lb = numberResultBefore.Length - 1;
            convN = null;
            if (numberResultBefore == "0")
            {
                convN = "0";
            }
            int zero = Convert.ToInt32(numberResultBefore[lb]);
            if (zero == 48)
            {
                while (lb != 0)
                {
                    convN += numberResultBefore[lb - 1];
                    lb--;
                }
            }
            else
            {
                while (lb != -1)
                {
                    convN += numberResultBefore[lb];
                    lb--;
                }
            }
            return convN;
        }
}