2

I am looking for suggestions to create a incremented ID (much like a license plate number). Instead of the usual 0-9 int32 type counter I am looking for something can go 0-9 then A-Z for each character in the sequence.

So I would have the potential to have a 6 char ID that would be '2A3DC3' This has the potential to give a much greater depth to the available values.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
D.E.Wright
  • 123
  • 4
  • Needing to be able to generate a ID that has a lot of dept in a very small space for user use. 36x36x36x36x36x36 gives me more flexibility in a small string than a 10 digit number would. Kind of like a car license plate number. – D.E.Wright Sep 12 '13 at 02:22
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Sep 12 '13 at 02:29

2 Answers2

2

You can create an auto-incremented Int32 ID and then format it in a base higher than 10 to get a short string. For example, have a look at Base36 (Wikipedia has C# code for both encoding and decoding), Base32 (ZBase32 is particularly designed for consumption by humans), or even Excel column names.

Community
  • 1
  • 1
dtb
  • 213,145
  • 36
  • 401
  • 431
  • This is looking to be the exact direction I am looking; just didn't have the right terminology. I will give it a shot... Thank you. – D.E.Wright Sep 12 '13 at 02:33
0

Here's some code which does it sequentially:

var allDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var @base = allDigits.Length;
var a = new StringBuilder();
var start = 2176782336;  // minimum 6 digit base36 number
var end = 76187381759;   // maximum 6 digit base36 number

Enumerable.Range(1, 1000)
          .Select(n => {
                           int r;
                           var d = n + start;
                           a.Clear();
                           do
                           {
                               r = (int)(d % @base);
                               d = d / @base;
                               a.Insert(0, allDigits[r]);
                           } while(d >= @base);

                           a.Insert(0, allDigits[r]);
                           return a.ToString();
                       })
          .Dump();
codekaizen
  • 26,990
  • 7
  • 84
  • 140