Convert.ToString() only allows base values of 2, 8, 10, and 16 for some odd reason; is there some obscure way of providing any base between 2 and 16?
Probably to eliminate someone typing a 7 instead of an 8, since the uses for arbitrary bases are few (But not non-existent).
Here is an example method that can do arbitrary base conversions. You can use it if you like, no restrictions.
string ConvertToBase(int value, int toBase)
{
if (toBase < 2 || toBase > 36) throw new ArgumentException("toBase");
if (value < 0) throw new ArgumentException("value");
if (value == 0) return "0"; //0 would skip while loop
string AlphaCodes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string retVal = "";
while (value > 0)
{
retVal = AlphaCodes[value % toBase] + retVal;
value /= toBase;
}
return retVal;
}
Untested, but you should be able to figure it out from here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With