Generate a unique string and number "GUID" in C # to go

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.