2

hey guys i want to generate unique base62 hashes - something similar to what tinyurl and bit.ly do using c#. this would be based on an auto increment field ID of type bigint (like most of these sites)

min chars would be 1 and max chars would be 6... if you had to write the fastest code (least amount of cpu usage) in c# for this hash how would you write it?

Raj
  • 6,810
  • 6
  • 48
  • 56

2 Answers2

3

Please see my answer to another Stack Overflow question which is similar, here:

Need a smaller alternative to GUID for DB ID but still unique and random for URL

I posted a C# class called "ShortCodes", that does exactly what you're looking for, i.e. generate a unique baseX (where X is anything you like!) hash based upon a integer/long number, and also to convert back again.

I actually wrote this little class precisely to mimic the short code/hash generation of sites like TinyUrl.com and Bit.ly for my own purposes.

I can't say if this is the absolute fastest way of achieving this, but it's not exactly slow either! :)

Community
  • 1
  • 1
CraigTP
  • 44,143
  • 8
  • 72
  • 99
0

Eric Lippert suggested lookup tables in a similar earlier question. His answer is perfect for your purposes as well.

Community
  • 1
  • 1
Joren
  • 14,472
  • 3
  • 50
  • 54