public static string FromBase10(ulong number, ushort toBase)
{
// If the to base is 10, simply return the number as a string
if (toBase == 10)
{
return number.ToString();
}
// The number has to be divided by the base it needs to be converted to
// until the result of the division is 0. The modulus of the division
// is used to calculate the character that represents it
StringBuilder runningResult = new StringBuilder();
while (number > 0)
{
ulong modulus = number % toBase;
if (modulus < 10)
{
runningResult.Insert(0, modulus);
}
else
{
runningResult.Insert(0, (char)('A' + modulus - 10));
}
number = (number - modulus) / toBase;
}
return runningResult.ToString();
}
}