Is there any way to create a short unique code like short GUID?

前端 未结 5 1945
予麋鹿
予麋鹿 2021-02-05 03:21

I want to create a short GUID. Is there any way to create a short unique code like short GUID? I want to create a ticket tracking number.

5条回答
  •  情深已故
    2021-02-05 04:11

    The length of GUID is 128bits(16bytes), so if you want to create a short GUID , you have to change GUID's encoding.

    For instance, you can use base64 or ASCII85.

        /// 
        /// Creates a GUID which is guaranteed not to equal the empty GUID
        /// 
        /// A 24 character long string
        public static string CreateGuid()
        {
            Guid guid = Guid.Empty;
            while (Guid.Empty == guid)
            {
                guid = Guid.NewGuid();
            }
    
            // Uses base64 encoding the guid.(Or  ASCII85 encoded)
            // But not recommend using Hex, as it is less efficient.
            return Convert.ToBase64String(guid.ToByteArray());
        }
    

提交回复
热议问题