Converting to Base62 & URL Shortening

Generic Marketeer: “Why do we need to shorten the URLs, people just click on them. It’s not like they have to memorize them; call them out to their friends”

Me: “Eh… no you’re right, but I do need to jam them into a SMS WAPPush along with some descriptive text, so I don’t have much room to play with”

Generic Marketeer: “Fine, as long as it doesn’t impact the project deadline”

Cue, my good self scurying away to find a way to bang out a private URL Shortening service in as short a time as possible. The URL Storage itself was a piece of pie. But I did stumble across some nice code while I was at it. I started out with a int-to-base64 implemenation. but the `+` & `/` characters are fugly to deal with in URLs. I tried a Hex version as well but it just didn’t look bit-ly-y enough. Enter the baseAnything encoder. Just point it at any character set and it will encode/decode to that number of chars.

The following extension methods convert longs to strings, and vice-versa

public static string ToBase(this long input, string baseChars)
    string r = string.Empty;
    int targetBase = baseChars.Length;
        r = string.Format("{0}{1}",
            baseChars[(int)(input % targetBase)],
        input /= targetBase;
    } while (input > 0);

    return r;

public static long FromBase(this string input, string baseChars)
    int srcBase = baseChars.Length;
    long id = 0;
    string r = input.Reverse();

    for (int i = 0; i < r.Length; i++)
        int charIndex = baseChars.IndexOf(r[i]);
        id += charIndex * (long)Math.Pow(srcBase, i);

    return id;

I’ve been using it against the first character set below, but you could easily tweak it to remove any “confusing” characters, or any set for that matter.

private static string ALPHANUMERIC =
    "0123456789" +

//Remove 0oO1iIl - Base52
private static string ALPHANUMERIC_ALT =
    "23456789" +

Now all I have to do is go write a Math Engine for it 😉

3 comments on “Converting to Base62 & URL Shortening

  1. Eoin

    So you have a byte array representation of a number ?

    It should be trivial enough then to just convert the byte array into a Int64 and wrap the above method. I don’t have an IDE at hand to check this but try

    public static string ToBase(this byte[] input, string baseChars)
    long l = BitConverter.ToInt64(input, 0);
    return ToBase(l, baseChars);

    Sorry it took so long to reply.

  2. Ben

    I got a runtime (Unable to cast object of type ‘d__a0`1[System.Char]’ to type ‘System.String’) error on the line:

    string r = input.Reverse();

    After converting the line to this, it now works:

    string r = new string(input.ToCharArray().Reverse().ToArray());

    I now have this as my FromBase method:

    using System.Linq;
    public static long FromBase(this string input, string baseChars)
    var srcBase = baseChars.Length;
    var r = new string(input.ToCharArray().Reverse().ToArray());
    return r.Select(t => baseChars.IndexOf(t)).Select((charIndex, i) => charIndex * (long)Math.Pow(srcBase, i)).Sum();

Leave a Reply

Your email address will not be published. Required fields are marked *