Skip to content

Instantly share code, notes, and snippets.

@danielrozo
danielrozo / gist:6048412
Created July 21, 2013 12:27
Extension methods for long to convert from/to Base 62. Useful for shrinking long numbers to small strings for URLs (bit.ly like).
public static string ToBase62(this long input)
{
string baseChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string r = string.Empty;
int targetBase = baseChars.Length;
do
{
r = string.Format("{0}{1}",
baseChars[(int)(input % targetBase)],
r);