当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Creating a complex UID thats not a GUID/UUID in the normal sense

问题描述:

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.

网友答案:

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.

网友答案:

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();
分享给朋友:
您可能感兴趣的文章:
随机阅读: