Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good idea to remove dash from a GUID?

Tags:

c#

guid

asp.net

I have code which replaces the dash with empty strings in a GUID.

Replace(System.Guid.NewGuid.ToString, "-", "") 

Would that remove the uniqueness of the guid?

Also can someone confirm this too?

We are storing GUIDs in our database as char(32). Is that okay? Because we have international users in different timezone, we're concerned that the character length could go over 32. Thanks.

like image 828
Salman Avatar asked Oct 20 '11 13:10

Salman


People also ask

Why do GUIDs have dashes?

That's just for convenience. GUID consists of 16 bytes which makes up 32 characters in hex text representation. Without hyphens GUIDs are harder to perceive by humans and harder to be recognized as GUIDs and not some random nature 16-byte numbers.

How many dashes are in a GUID?

Guid should contain 32 digits with 4 dashes.

Can GUID be empty c#?

If you want to create an empty Guid, you can use Guid. Empty : it will return a Guid composed only by 0s, like 00000000-0000-0000-0000-000000000000.

How many bits is a GUID?

A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.


2 Answers

The dashes are always in the same place, so no, it will not reduce the uniqueness.

System.Guid.NewGuid().ToString("N"); 

Will generate a GUID without dashes, as in this documentation

like image 66
tonycoupland Avatar answered Oct 09 '22 23:10

tonycoupland


Yes, it is OK to remove the dashes. The uniqueness of the Guid is guaranteed.

Dashes are only for readability: internally the Guid is made of 16 bytes.
You can see Microsoft and Wikipedia for more details.
Take a look at constructors too:

public Guid(int a, short b, short c, byte[] d); public Guid(int a, short b, short c, byte d,              byte e, byte f, byte g, byte h,              byte i, byte public Guid(uint a, ushort b, ushort c, byte d,              byte e, byte f, byte g, byte h,              byte i, byte j, byte k); 
like image 24
Marco Avatar answered Oct 09 '22 23:10

Marco