69

Why are there dashes in a .NET GUID? Are there dashes in most implementations of a GUID, or is it just a Microsoft thing?

Signed,

741ecf77-9c92-4435-8e6b-85975bd13452

3
  • Interesting question. I must say that I've never stopped to think about it. Jan 6, 2009 at 16:18
  • GUIDs have the same amount of entropy regardless of whether they have hyphens in them.
    – recursive
    Aug 24, 2010 at 14:16
  • The hyphens are only used when displaying one as text. The real UUID is a binary string/array of 16 bytes. Sep 23, 2010 at 22:05

13 Answers 13

66

Technically, there are no "dashes" in a GUID. A GUID is a 128-bit value which is usually stored in the following manner (using C# here to represent the structure):

public struct Guid
{
  public ulong Data1;
  public ushort Data2;
  public ushort Data3;
  public fixed byte Data4[8];
}

The dashes are in the string representation of a GUID.

The dashes are optional and are not required in a string representation of a GUID.

That said, there are historical reasons as to where the placement of the dashes are, related to how the GUIDs were generated, but that historical semantic no longer applies.

4
  • 3
    This is just totally and completely incorrect, there are 5 hyphenated elements in hyphenated ASCII Hex representation of a GUID, the struct that contains the elements of binary GUID is only 4 elements long. Also, the historical semantics definitely do still apply, especially in this question, considering that history is the exact reason why the hyphens are there in the first place.
    – joshperry
    Dec 26, 2012 at 15:13
  • 2
    @joshperry You've reiterated my point from the answer. A Guid is just 128 bytes, period. The separators are from the representation. Also, in regards to the historical semantics applying in the representation, they don't. As you said, it's for historical reasons. Those historical reasons don't apply anymore. From your answer, "these data elements no longer have any specific meaning."
    – casperOne
    Dec 26, 2012 at 16:04
  • 2
    I'm not sure where I reiterated that a GUID is 128 bytes, but your second paragraph (about the hyphens having correspondence to the struct) is patently inaccurate. The hyphens are there because that layout is specified in the UUID RFC and has nothing to do with that structure. It is true that the segments in contemporary UUIDs do not necessarily mean what they used to, however, they are there because of what they used to mean. So I guess they are now there because the RFC says so and if you want/need to create interoperable URNs then you'll follow the RFC.
    – joshperry
    Dec 26, 2012 at 17:06
  • 1
    @joshperry Removed the part about the placement of the dashes, and left how they are optional. That should satisfy your point.
    – casperOne
    Dec 26, 2012 at 17:14
46

In the initial version of the UUID (Universally Unique Identifier) specification, each of the data elements had a semantic meaning:

{ time_low } – { time_mid } – { time_high_and_version } – { clock_seq_and_reserved clock_seq_low } – { node_id }

These elements were designed to provide temporal (time bits), and spatial (host bits) uniqueness.

Version History

As the the mathematical probability of collisions in a keyspace of 2^1024 random bits was found to be astronomically improbable, subsequent versions of the UUID spec have phased out the time and host data for security and privacy reasons.

The only elements that retain any meaning are the version bits and the reserved bits.

Version 3 UUIDs are derived from an MD5 hash of a URI or other Distinguished Name.

Version 4 is generated with random data and is, currently, the most common implementation you'll see in the wild.

Version 5 is derived from a SHA1 hash.

Storage formats

Since the hyphens are specified for the ASCII formatting of UUIDs in the RFC, even though the individual sections no longer retain their original meaning, they are still required if you need interoperability.

UUIDs are also sometimes stored as a base64 or ascii85 encoded string to save space for transmission over transports that are not binary-safe, and adherence to the RFC is not required.

Ascii:   3F2504E0-4F89-11D3-9A0C-0305E82C3301
Base64:  7QDBkvCA1+B9K/U0vrQx1A
Ascii85: 5:$Hj:Pf\4RLB9%kU\Lj

References:
RFC4122 (see page 3 specifically for the ABNF description of the UUID format)
Wikipedia GUID UUID

14

You can get your guid in various formats.

Assuming you're using c#:

Guid guid = Guid.NewGuid();

Console.WriteLine(guid.ToString("N"))

63be6f7e4e564f0580229f958f492077

Console.WriteLine(guid.ToString("D"))

63be6f7e-4e56-4f05-8022-9f958f492077

Console.WriteLine(guid.ToString("B"))

{63be6f7e-4e56-4f05-8022-9f958f492077}

Console.WriteLine(guid.ToString("P"))

(63be6f7e-4e56-4f05-8022-9f958f492077)

1
13

Hyphens denote the byte structure of a Guid.

typedef struct _GUID 
{  
   DWORD Data1;  
   WORD Data2;  
   WORD Data3;  
   BYTE Data4[8];
} GUID;

For:

(XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX)

You can probably strip them before saving. At least in .NET the constructor of the Guid type will initialize a Guid variable from its string representation regardless of whether the hyphens are still there or removed.

3
  • Well, that's true, but you can't use parts of that struct in any meaningful way, can you?
    – sharptooth
    Aug 24, 2010 at 14:20
  • But why the extra hyphen within Data4?
    – dan04
    Aug 24, 2010 at 14:55
  • Look here for an explanation: en.wikipedia.org/wiki/Globally_unique_identifier
    – user151323
    Aug 24, 2010 at 15:05
9

It's just a convenience.

http://en.wikipedia.org/wiki/GUID

6

This is an example of chunking, just like phone numbers, credit card numbers, etc.

Here is a good Wikipedia article about it.

4

Just about every visual represenation of a guid that I've seen uses the dashed format. It's much easier on the eyes.

3

The Guid class of .NET recognizes a bunch of different formats: dashes as separators, no separators, brackets as delimiters, parenthesis as delimiters, no delimiters, etc

1
  • Correction, a Guid is not a class, it's a value type.
    – casperOne
    Dec 18, 2012 at 18:18
2

The hyphens are used to separate each number

E93416C5-9377-4A1D-8390-7E57D439C9E7

Hex digits  Description
8           Data1
4           Data2
4           Data3
4           Initial two bytes from Data4
12          Remaining six bytes from Data4
1

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.

0

If you want to store a guid somewhere, then store it as an array of 16 bytes, not as its textual representation. You will save a lot of space, and the question of hyphens will not arise.

0

The GUID is really just a number. The hyphens show you how the various components are broken down but aren't really part of the number. It's like an IP address - you can store a 32-bit number, or you can store a string with dots in it, they are equivalent.

0

The hypens have avsolutely no effect on the uniqueness or randomness of the value. They are merely a holdover from the definition of a GUID and visually separate the four distinct parts of data that make up the GUID.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.