I wrote a small method for convert number from base-10 number system to any number system (code is not perfect but that is not the point). My question is if I get right single responsibility principle. Is 2nd approach better than 1st or 'charReplace' functionality is too small for creating own method ?
public static string convertNumber(BigInteger num, int baseNum)
{
string symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
if (baseNum < 2 || symbols.Length + 10 < baseNum)
{
throw new Exception;
}
var result = "";
do
{
var partResult = (int)(num % baseNum);
if (partResult >= 10)
{
result = result.Insert(0, symbols[partResult - 10].ToString());
}
else
{
result = result.Insert(0, partResult.ToString());
}
num /= baseNum;
} while (num != 0);
return result;
}
vs.
static readonly string Symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public static string convertNumber(BigInteger num, int baseNum)
{
if (baseNum < 2 || Symbols.Length + 10 < baseNum)
{
throw new Exception;
}
var result = "";
do
{
var partResult = (int)(num % baseNum);
result = result.Insert(0, charReplace(partResult));
num /= baseNum;
} while (num != 0);
return result;
}
private static string charReplace(int number){
if (number < 10){
return number.ToString();
}
return Symbols[number - 10].ToString();
}
BigInteger
is not exactly a "base-10" number. It is just abstractly a number. While it's true thatBigInteger.Parse()
andBigInteger.ToString()
both work in base ten, theBigInteger
itself isn't in any particular base. \$\endgroup\$