C # generate a unique number based on the string

Source: Internet
Author: User

Generate a unique numeric ID based on the string. Generally, GUID is used. MD5 is calculated based on the string. The calculated MD5 code is a guid.

However, The GUID is too long. It is a 128-bit number. during storage, it takes a lot of space and determines whether to generate a shorter number, such as a 32-bit number, someone has provided this method.

: We can set a standard guid21726045-e8f7-4b09-abd8-4bcc926e9e28Convert to a short string3c4ebc5f5f2c4edc

The following method generates a short string that is unique. Repeat is not repeated for 0.1 billion times. It also generates the string according to the guid uniqueness.

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 numerical sequence instead of a string, you will get a 19-bit long sequence. The following method converts a guid to an int64 numeric sequence.

Private   Long Generateintid ()
{
Byte [] Buffer = Guid. newguid (). tobytearray ();
Return Bitconverter. toint64 (buffer, 0 );
}

 

it seems that this method is feasible, but no rigorous data demonstration is found for the time being to ensure high non-conflict performance. after all, the key value is used. If there is a duplicate value, the Program will be suspended.

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.