Converting a decimal from base 10 to base 62 in C#
June 10th, 2008 LukeFor a recent project, we needed to store as many possible values into 3 characters as we could, based on a numeric ID. So, we created a function that would convert that ID to base 62, which we could then use to increase the number of available combinations that we could store. Here’s the function:
public const string base62digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
public string ToBase62(int input)
{
int modulo;
string sTemp = "";
do
{
modulo = input % 62;
sTemp = base62digits.Substring(modulo + 1, 1) + sTemp;
input = input / 62;
} while (input != 0);
return sTemp;
}
It’s been modified from the VB code to do the same thing, which you can find here.