Creating a unique URL safe hash

I want to hash/encode a unique integer (database ID) to create a similarly unique string.

It needs to meet the following requirements:

  1. Must start with a letter or number, and can contain only letters and numbers.
  2. All letters in a container name must be lowercase.
  3. Must be from 3 through 63 characters long (although the shorter the better)

The result does not need to be reversible, just repeatable - so a 1-way hash would be fine.

Answers


A simple solution would be a base 36 encoding. The output will be a string between one and six characters.

public static string EncodeBase36(int i)
{
  Contract.Requires<ArgumentException>(i>=0);
  //Base conversion
  string s="";
  while(i!=0)
  {
    int digit = i % 36;
    i/=36;
    if(digit<10)
      s=((char)('0'+digit)).ToString()+s;
    else
      s=((char)('a'+digit-10)).ToString()+s;
  }
  // Enforce minimum length
  while(s.Length<3)
  {
    s = "0" + s;
  }
  return s;
}

Need Your Help

Scheme call/cc issue - implementing exit.

scheme callcc

I am writing a program that needs to recursively analyze a list of "commands" and "programs" (it is an interpreter of some "robotic language" invented by our professor for a robot living in a maze).

Parsing Binary Data in C?

c parsing network-programming

Are there any libraries or guides for how to read and parse binary data in C?

rails handling 404 on webrick getting ActiveRecord::RecordNotFound - simple questioon

ruby-on-rails ruby-on-rails-3

So, I have a catch all route that is going to act like a vanity url piece. So, I have a call: