Compress the guid to 26 bits using a 36-digit system.

Source: Internet
Author: User

Msdn explanation:

GUID is a 128-bit integer (16 bytes) that can be used on all computers and networks that require a unique identifier. This identifier is unlikely to be repeated.

Uint64 indicates a 64-bit unsigned integer. Value Type indicates an unsigned integer between 0 and 18,446,744,073,709,551,615.

The principle is to split the guid into two 64-bit uint64 characters, and then convert the uint64 into a 36-digit character format.

The maximum value of uint64 is converted to 36 in hexadecimal notation of 13 BITs. Therefore, the generated characters are 26 characters in length.

ConversionCodeAs follows:

/// <Summary> /// Brief description of convert36 /// </Summary> public class convert36 {public convert36 () {// todo: add the constructor logic here //} private const string base_char = "0123456789 abcdefghijklmnopqrstuvwxyz"; // convert it to the segment character Private Static string getlongno (uint64 num, int length) {string STR = ""; while (Num> 0) {int cur = (INT) (Num % 36); STR = base_char [cur] + STR; num = num/36;} If (Str. length> length) {STR = Str. substring (Str. le Ngth-length);} else {STR = Str. padleft (length, '0');} return STR;} // resolution segment character Private Static uint64 getlongnum (string strno) {uint64 num = 0; For (INT I = 0; I <strno. length; I ++) {num + = (uint64) base_char.indexof (strno [I]) * (uint64) math. pow (base_char.length, strno. length-I-1);} return num ;} /// <summary> /// compress the guid /// </Summary> /// <Param name = "G"> </param> /// <returns> </returns> Public static string get Guidno (guid g) {string S = G. tostring (). replace ("-",""). toupper (); string S1 = S. substring (0, 16); string S2 = S. substring (16); uint64 L1 = uint64.parse (S1, system. globalization. numberstyles. hexnumber); uint64 L2 = uint64.parse (S2, system. globalization. numberstyles. hexnumber); string str1 = getlongno (L1, 13); string str2 = getlongno (L2, 13); Return str1 + str2 ;} /// <summary> /// obtain the guid /// </Summary> /// <p Aram name = "str"> </param> // <returns> </returns> Public static guid getguid (string Str) {If (Str. length! = 26) {Throw new exception ("string error! The length must be 26 characters! ");} String S1 = Str. substring (0, 13); string S2 = Str. substring (13); uint64 L1 = getlongnum (S1); uint64 L2 = getlongnum (S2); string str1 = l1.tostring ("X "); string str2 = l2.tostring ("X"); string strguid = str1.padleft (16, '0'); strguid + = str2.padleft (16, '0 '); guid G = new GUID (strguid); Return g ;}}

Instance download: http://files.cnblogs.com/zjfree/Convert36.rar

 

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.