public static ulong ToBase10(string encodedNumber, ushort fromBase)
{
// If the from base is 10, simply parse the string and return it
if (fromBase == 10)
{
return UInt64.Parse(encodedNumber);
}
// Ensure that the string only contains upper case characters
encodedNumber = encodedNumber.ToUpper();
// Go through each character and decode its value.
int length = encodedNumber.Length;
ulong runningTotal = 0;
for (int index = 0; index < length; index++)
{
char currentChar = encodedNumber[index];
// Anything above base 10 uses letters as well as numbers, so A will be 10, B will be 11, etc.
uint currentValue = Char.IsDigit(currentChar) ? (uint)(currentChar - '0') :
(uint)(currentChar - 'A' + 10);
// The value which of the character represents depends on its position and it is calculated
// by multiplying its value with the power of the base to the position of the character, from
// right to left.
runningTotal += currentValue * (ulong)Math.Pow(fromBase, length - index - 1);
}
return runningTotal;
}