Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

guid

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.

like image 298
masoud ramezani Avatar asked Nov 14 '11 06:11

masoud ramezani


People also ask

Is there a short GUID?

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

How can a GUID be unique?

How unique is unique? A GUID is a unique number that can be used as an identifier for anything in the universe, but unlike ISBN there is no central authority - the uniqueness of a GUID relies on the algorthm that was used to generate it.

Will we run out of GUIDs?

Absolutely. Even if only one GUID is generated per second, we'll run out in a scant 9 quintillion years.

How do I generate a GUID?

To Generate a GUID in Windows 10 with PowerShell, Type or copy-paste the following command: [guid]::NewGuid() . This will produce a new GUID in the output. Alternatively, you can run the command '{'+[guid]::NewGuid(). ToString()+'}' to get a new GUID in the traditional Registry format.


1 Answers

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.

    /// <summary>     /// Creates a GUID which is guaranteed not to equal the empty GUID     /// </summary>     /// <returns>A 24 character long string</returns>     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());     } 
like image 187
JKhuang Avatar answered Oct 21 '22 03:10

JKhuang