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;
}
}
namespace Solution {
using NUnit.Framework;
using System;
// TODO: Replace examples and use TDD by writing your own tests
[TestFixture]
public class SolutionTest
{
[Test]
public void Test()
{
Assert.AreEqual("254", NumberConverter.Converting(10, 10, "254"));
Assert.AreEqual("100101000100010", NumberConverter.Converting(12, 2, "AB96"));
Assert.AreEqual("343", NumberConverter.Converting(2, 7, "10110010"));
Assert.AreEqual("3CA626C", NumberConverter.Converting(10, 16, "63595116"));
Assert.AreEqual("2C9D", NumberConverter.Converting(7, 15, "36661"));
Assert.AreEqual("1030021101", NumberConverter.Converting(13, 4, "ABC66"));
Assert.AreEqual("0", NumberConverter.Converting(10, 2, "0"));
Assert.AreEqual("2B943087", NumberConverter.Converting(16, 14, "11CD1A7F"));
}
}
}