Today, I came across an overload of the Convert.ToString method which surprisingly accepted a parameter called toBase. I wish I had come across this earlier... The overload accepts the bases 2, 8, 10 or 16 and throws an ArgumentException if one of these are not passed in. Anyway, I converted the method I wrote to an extension method on Int32 called "InBase" - I think that reads better than Convert.ToString with a base passed in. The implementation is below in case you want to include it in your libraries.
Example usage:
public static string InBase(this int number, int @base)
{
return Convert.ToString(number, @base);
}
Console.WriteLine("{0}", 10.InBase(2)); // Outputs "1010" (Binary)
Console.WriteLine("{0}", 10.InBase(8)); // Outputs "12" (Octal)
Console.WriteLine("{0}", 10.InBase(10)); // Outputs "10" (Decimal)
Console.WriteLine("{0}", 10.InBase(16)); // Outputs "a" (Hexadecimal)
Console.WriteLine("{0}", 10.InBase(3)); // Throws ArgumentException