Transferred from: http://www.cnblogs.com/lcwzj/archive/2009/04/16/1436992.html
GUIDs are often thought of when we want to get a unique key. This key is very long, although in many cases this is not a problem. But when we need to put this 36-character string in the URL, it makes the URL very ugly.
It is not possible to shorten the length of the GUID without sacrificing its uniqueness, but it is possible to make this sacrifice if we can accept a 16-bit string.
We can convert a standard GUID 21726045-e8f7-4b09-abd8-4bcc926e9e28 to a short string 3c4ebc5f5f2c4edc
The following method generates a short string, and the string is unique. Repeating 100 million times is not duplicated, it also generates the string according to the uniqueness of the 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);
If you want to generate a sequence of numbers instead of strings, you will get a 19-bit long sequence. The following method converts the GUID to a sequence of Int64 numbers.
Private Long Generateintid ()
{
byte[] buffer = Guid.NewGuid (). Tobytearray ();
Return Bitconverter.toint64 (buffer, 0);
}
Generate a unique string and number "GUID" in C # to go