惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
罗磊的独立博客
J
Java Code Geeks
博客园_首页
量子位
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Security Latest
Security Latest
Cloudbric
Cloudbric
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
雷峰网
雷峰网
Apple Machine Learning Research
Apple Machine Learning Research
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
Cybersecurity and Infrastructure Security Agency CISA
L
Lohrmann on Cybersecurity
爱范儿
爱范儿
月光博客
月光博客
Spread Privacy
Spread Privacy
T
Tailwind CSS Blog
C
CERT Recently Published Vulnerability Notes
S
Schneier on Security
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
美团技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 司徒正美
The Cloudflare Blog
G
GRAHAM CLULEY
I
Intezer
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta

博客园 - YQM

NUnit单元测试属性介绍 C#静态构造函数 没有使用lock的Singleton How to serialize and deserialize using C# .NET - YQM Javascript调用web service 简单的英语不简单 我们应该怎样去学习和工作--写给自己 扩展方法 使用反射-动态创建对象及调用对象方法 使用XmlWriter对象 使用XmlReader类 知道做什么而不是怎样做(摘抄) 生活15点,要记住! 设计模式之Command模式 使用javascript动态添加和删除table的行和列 Some notes Localization in ASP.NET 2.0 ASP.NET 2.0: Playing a bit with GridView "Sort Grouping" 通过TryParse来检验和转换数据类型
Generate unique strings and numbers in C#(生成一个唯一的字符串和数值)
YQM · 2007-05-26 · via 博客园 - YQM

The System.Guid is used whenever we need to generate a unique key, but it is very long. That's in many cases not an issue, but in a web scenario where it is part of the URL we need to use its string representation which is 36 characters long. It clutters up the URL and is just basically ugly.

It is not possible to shorten it without loosing some of the uniqueness of the GUID, but we can come a long way if we can accept a 16 character string instead.

We can change the standard GUID string representation:

21726045-e8f7-4b09-abd8-4bcc926e9e28

Into a shorter string:

3c4ebc5f5f2c4edc

The following method creates the shorter string and it is actually very unique. An iteration of 10 million didn.t create a duplicate. It uses the uniqueness of a GUID to create the string.

private string GenerateId()
{
 
long i = 1;
 
foreach (byte b in Guid.NewGuid().ToByteArray())
 
{
  i 
*= ((int)b + 1);
 }

 
return string.Format("{0:x}", i - DateTime.Now.Ticks);
}

If you instead want numbers instead of a string, you can do that to but then you need to go up to 19 characters. The following method converts a GUID to an Int64.

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

The standard GUID is still the best way to ensure the uniqueness even though it isn't 100% unique. (加上年月日小时分秒,对90%的系统来说,it is 1000% unique了。)
原文地址:http://www.csharphelp.com/archives4/archive691.html  (By Mads Kristensen)