• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

GUID字符串,16位字符串,19位数字在C#中生成唯一的字符串和数字 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

      当我们想要获得一个唯一的key的时候,通常会想到GUID。这个key非常的长,虽然我们在很多情况下这并不是个问题。但是当我们需要将这个36个字符的字符串放在URL中时,会使的URL非常的丑陋。

      想要缩短GUID的长度而不牺牲它的唯一性是不可能的,但是如果我们能够接受一个16位的字符串的话是可以做出这个牺牲的。

      我们可以将一个标准的GUID 21726045-e8f7-4b09-abd8-4bcc926e9e28  转换成短的字符串 3c4ebc5f5f2c4edc

      下面的方法会生成一个短的字符串,并且这个字符串是唯一的。重复1亿次都不会出现重复的,它也是依照GUID的唯一性来生成这个字符串的。

        private string GenerateStringID()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }

      如果你想生成一个数字序列而不是字符串,你将会获得一个19位长的序列。下面的方法会把GUID转换为Int64的数字序列。

        private long GenerateIntID()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }

1)假设数据库是这个样子的:

 

2)运行以下控制台代码:

static void Main(string[] args)
        {
            //测试bigint的读取:
            using (SqlConnection con = new SqlConnection(@"server=.\sqlexpress;database=MyTest;integrated security=true"))
            {
                SqlCommand cmd = new SqlCommand("select top 1 number from tb_dbo", con);
                con.Open();
                IDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleRow);
                dr.Read();
                long n = Convert.ToInt64(dr["number"]);
                Console.WriteLine(n);
                dr.Close();
            }
        }

 

3)结果如下:

 

 

是否可以无损耗的压缩GUID为12位呢?

当我们想要获得一个唯一的key的时候,通常会想到GUID。这个key的长度是36位,如果将这个36为的字符串存储或是用url传递的时候就会感觉非常的难看。
就算去掉-分隔符也有32位,如 EAA82B2DA9EA4E5B95330BAF9944FB35,如果转为数字序列 如将guid转为int64数字序列,长度也会有19位。
byte[] buffer = Guid.NewGuid().ToByteArray();  
long long_guid=BitConverter.ToInt64(buffer, 0);
这样就会得到一个类似于  5472976187161141196 的19位长度的 数字序列。
如果我们将 5472976187161141196 分解为 54 72 97 61 87 161 141 196,应该可以用8个字符就可以显示,但会有一部分是不可显示的字符。
如果将这8个字符转为base64,发现只需要10-14个为就能显示完毕,将一些url用到的某些符号剔除,通常会产生12位的编码较多,10位的编码较少。
经过100万次的测试,没有发现会有重复的字符产生,不知道是否是完美的将guid 压缩为12位的方法呢?
如果你有更好的做法,可以共享出来。
附源代码
 public static string UUID()
        {
            
byte[] buffer = Guid.NewGuid().ToByteArray();  
            
long long_guid=BitConverter.ToInt64(buffer, 0);

            
string _Value = System.Math.Abs(long_guid).ToString();

            
byte[] buf=new byte[_Value.Length];
            
int p=0;
            
for (int i = 0; i < _Value.Length;)
            {
                
byte ph=System.Convert.ToByte(_Value[i]);

                
int fix=1;
                
if ((i+1)<_Value.Length)
                {
                    
byte pl=System.Convert.ToByte(_Value[i+1]);
                    buf[p]
=(byte)((ph<<4)+pl);
                    fix
=2;
                }
else{
                    buf[p]
=(byte)(ph);
                }

                
if ((i+3)<_Value.Length)
                {
                    
if (System.Convert.ToInt16(_Value.Substring(i,3))<256)
                    {
                        buf[p]
=System.Convert.ToByte(_Value.Substring(i,3));
                        fix
=3;
                    }
                }
                p
++;
                i
=i+fix;
            }
            
byte[] buf2=new byte[p];
            
for (int i=0;i<p;i++)
            {
                buf2[i]
=buf[i];
            }
            
string cRtn=System.Convert.ToBase64String(buf2);
            
if (cRtn==null)
            {
                cRtn
="";
            }
            cRtn
=cRtn.ToLower();
            cRtn
=cRtn.Replace("/","");
            cRtn
=cRtn.Replace("+","");
            cRtn
=cRtn.Replace("=","");
            
if (cRtn.Length==12
            {
                
return cRtn;
            }
else{
               
return UUID();
            }
        }

 

 

 

      当我们想要获得一个唯一的key的时候,通常会想到GUID。这个key非常的长,虽然我们在很多情况下这并不是个问题。但是当我们需要将这个36个字符的字符串放在URL中时,会使的URL非常的丑陋。

      想要缩短GUID的长度而不牺牲它的唯一性是不可能的,但是如果我们能够接受一个16位的字符串的话是可以做出这个牺牲的。

      我们可以将一个标准的GUID 21726045-e8f7-4b09-abd8-4bcc926e9e28  转换成短的字符串 3c4ebc5f5f2c4edc

      下面的方法会生成一个短的字符串,并且这个字符串是唯一的。重复1亿次都不会出现重复的,它也是依照GUID的唯一性来生成这个字符串的。

        private string GenerateStringID()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
            {
                i *= ((int)b + 1);
            }
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }

      如果你想生成一个数字序列而不是字符串,你将会获得一个19位长的序列。下面的方法会把GUID转换为Int64的数字序列。

        private long GenerateIntID()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }

1)假设数据库是这个样子的:

 

2)运行以下控制台代码:

static void Main(string[] args)
        {
            //测试bigint的读取:
            using (SqlConnection con = new SqlConnection(@"server=.\sqlexpress;database=MyTest;integrated security=true"))
            {
                SqlCommand cmd = new SqlCommand("select top 1 number from tb_dbo", con);
                con.Open();
                IDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleRow);
                dr.Read();
                long n = Convert.ToInt64(dr["number"]);
                Console.WriteLine(n);
                dr.Close();
            }
        }

 

3)结果如下:

 

 

是否可以无损耗的压缩GUID为12位呢?

当我们想要获得一个唯一的key的时候,通常会想到GUID。这个key的长度是36位,如果将这个36为的字符串存储或是用url传递的时候就会感觉非常的难看。
就算去掉-分隔符也有32位,如 EAA82B2DA9EA4E5B95330BAF9944FB35,如果转为数字序列 如将guid转为int64数字序列,长度也会有19位。
byte[] buffer = Guid.NewGuid().ToByteArray();  
long long_guid=BitConverter.ToInt64(buffer, 0);
这样就会得到一个类似于  5472976187161141196 的19位长度的 数字序列。
如果我们将 5472976187161141196 分解为 54 72 97 61 87 161 141 196,应该可以用8个字符就可以显示,但会有一部分是不可显示的字符。
如果将这8个字符转为base64,发现只需要10-14个为就能显示完毕,将一些url用到的某些符号剔除,通常会产生12位的编码较多,10位的编码较少。
经过100万次的测试,没有发现会有重复的字符产生,不知道是否是完美的将guid 压缩为12位的方法呢?
如果你有更好的做法,可以共享出来。
附源代码
 public static string UUID()
        {
            
byte[] buffer = Guid.NewGuid().ToByteArray();  
            
long long_guid=BitConverter.ToInt64(buffer, 0);

            
string _Value = System.Math.Abs(long_guid).ToString();

            
byte[] buf=new byte[_Value.Length];
            
int p=0;
            
for (int i = 0; i < _Value.Length;)
            {
                
byte ph=System.Convert.ToByte(_Value[i]);

                
int fix=1;
                
if ((i+1)<_Value.Length)
                {
                    
byte pl=System.Convert.ToByte(_Value[i+1]);
                    buf[p]
=(byte)((ph<<4)+pl);
                    fix
=2;
                }
else{
                    buf[p]
=(byte)(ph);
                }

                
if ((i+3)<_Value.Length)
                {
                    
if (System.Convert.ToInt16(_Value.Substring(i,3))<256)
                    {
                        buf[p]
=System.Convert.ToByte(_Value.Substring(i,3));
                        fix
=3;
                    }
                }
                p
++;
                i
=i+fix;
            }
            
byte[] buf2=new byte[p];
            
for (int i=0;i<p;i++)
            {
                buf2[i]
=buf[i];
            }
            
string cRtn=System.Convert.ToBase64String(buf2);
            
if (cRtn==null)
            {
                cRtn
="";
            }
            cRtn
=cRtn.ToLower();
            cRtn
=cRtn.Replace("/","");
            cRtn
=cRtn.Replace("+","");
            cRtn
=cRtn.Replace("=","");
            
if (cRtn.Length==12
            {
                
return cRtn;
            }
else{
               
return UUID();
            }
        }

 

 

 

当我们想要获得一个唯一的key的时候,通常会想到GUID。这个key的长度是36位,如果将这个36为的字符串存储或是用url传递的时候就会感觉非常的难看。
就算去掉-分隔符也有32位,如 EAA82B2DA9EA4E5B95330BAF9944FB35,如果转为数字序列 如将guid转为int64数字序列,长度也会有19位。
byte[] buffer = Guid.NewGuid().ToByteArray();  
long long_guid=BitConverter.ToInt64(buffer, 0);
这样就会得到一个类似于  5472976187161141196 的19位长度的 数字序列。
如果我们将 5472976187161141196 分解为 54 72 97 61 87 161 141 196,应该可以用8个字符就可以显示,但会有一部分是不可显示的字符。
如果将这8个字符转为base64,发现只需要10-14个为就能显示完毕,将一些url用到的某些符号剔除,通常会产生12位的编码较多,10位的编码较少。
经过100万次的测试,没有发现会有重复的字符产生,不知道是否是完美的将guid 压缩为12位的方法呢?
如果你有更好的做法,可以共享出来。
附源代码
 public static string UUID()
        {
            
byte[] buffer = Guid.NewGuid().ToByteArray();  
            
long long_guid=BitConverter.ToInt64(buffer, 0);

            
string _Value = System.Math.Abs(long_guid).ToString();

            
byte[] buf=new byte[_Value.Length];
            
int p=0;
            
for (int i = 0; i < _Value.Length;)
            {
                
byte ph=System.Convert.ToByte(_Value[i]);

                
int fix=1;
                
if ((i+1)<_Value.Length)
                {
                    
byte pl=System.Convert.ToByte(_Value[i+1]);
                    buf[p]
=(byte)((ph<<4)+pl);
                    fix
=2;
                }
else{
                    buf[p]
=(byte)(ph);
                }

                
if ((i+3)<_Value.Length)
                {
                    
if (System.Convert.ToInt16(_Value.Substring(i,3))<256)
                    {
                        buf[p]
=System.Convert.ToByte(_Value.Substring(i,3));
                        fix
=3;
                    }
                }
                p
++;
                i
=i+fix;
            }
            
byte[] buf2=new byte[p];
            
for (int i=0;i<p;i++)
            {
                buf2[i]
=buf[i];
            }
            
string cRtn=System.Convert.ToBase64String(buf2);
            
if (cRtn==null)
            {
                cRtn
="";
            }
            cRtn
=cRtn.ToLower();
            cRtn
=cRtn.Replace("/","");
            cRtn
=cRtn.Replace("+","");
            cRtn
=cRtn.Replace("=","");
            
if (cRtn.Length==12
            {
                
return cRtn;
            }
else{
               
return UUID();
            }
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#.NETentity下sqlite部署问题发布时间:2022-07-13
下一篇:
C#IOCPServer异步I/O模型发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap