93

Using ASCII encoding, how many characters are there in a GUID?

I'm interested in the Microsoft style, which includes the curly brackets and dashes.

4
  • 2
    They're all exactly the same length. If you count the characters in one, you'll know the length of all of them. Mar 3, 2009 at 19:04
  • 8
    When I googled this question, every answer just pointed to a definition of a guid. Downvote me if you want, but I'm just trying to make the answer to this sepceific question easier to find, with no counting needed for future askers.
    – Jim Counts
    Mar 3, 2009 at 19:12
  • 2
    I understand what you're saying, but if someone has just been introduced to NewDataX, the question as to length is valid, and you can't assume that what you've seen exemplifies every other NewDataX until you understand what a NewDataX is.
    – Adam Davis
    Mar 3, 2009 at 19:13
  • I don't see where anyone mentioned anything called a "NewDataX".
    – Oscar
    Jan 29, 2011 at 21:22

4 Answers 4

139

From MSDN:

A GUID is a 128-bit value consisting of one group of 8 hexadecimal digits, followed by three groups of 4 hexadecimal digits each, followed by one group of 12 hexadecimal digits. The following example GUID shows the groupings of hexadecimal digits in a GUID: 6B29FC40-CA47-1067-B31D-00DD010662DA

From Wikipedia:

Often braces are added to enclose the above format, as such:

{3F2504E0-4F89-11D3-9A0C-0305E82C3301}

So a total of 38 characters in the typical hexadecimal encoding with curly braces.

-Adam

5
  • @magnifico -- see the last paragraph of this answer: "So a total of 38 characters in the typical hexadecimal encoding with curly braces."
    – Randolpho
    Mar 3, 2009 at 19:09
  • 1
    No need to fight with magnifico - he asked the question, he gets to decide who gave the most relevant answer. He has his reasons for preferring the other answer to this one, I'm sure.
    – Adam Davis
    Mar 3, 2009 at 19:09
  • 1
    @Beska because that's what Stack Overflow is for. Instead of handing one person a fish, hand the same fish to a million people.
    – bugloaf
    Aug 27, 2013 at 3:08
  • @bugloaf Hmm. I suspect both of my comments were in reference to something that has has been deleted, since they made no sense in the current context...and the first one was very specifically a response to something gone. I've gotten rid of them. (In the current context it looked like I was railing against the answer, which is a good answer, and which would make no sense for me to do, and which you were correctly calling out.)
    – Beska
    Aug 27, 2013 at 13:02
  • @AdamDavis I count 32 not 38. edit: you must be counting the braces and dashes, nevermind.
    – Kyle
    Nov 26, 2013 at 16:09
33

TL;DR: None.

As Adam Davis stated, the Microsoft style is HEX encoding (with braces and dashes to make it more readable) that can be displayed using a subset of ASCII characters (0-9 and A-F), but this is not specifically ASCII encoding.

I guess it's important to remember that the microsoft style of displaying GUID's is only a representation of a GUID, which is actually a 16 byte integral value (as Micheal Trausch stated).

You can also present it in different, more compact ways by converting the bytes into a different character set (like ASCII).

Theoretically you can display each byte as an extended ASCII character (255 characters), which would allow you to save a GUID as a 16 character length string.

It wouldn't be very readable though because it would include whitespace characters (CR, space, tab, etc) and other special characters, so this would only make sense if you want to efficiently save a GUID in a non-human readable character format, for example in in a database that doesn't natively support GUID's or fast matching of small binary values: http://en.wikipedia.org/wiki/Extended_ASCII

IMHO the most readable way to display a GUID more compact would be to use Base64 encoding, which allows you to save it in a string with a length of 22 characters, and would make it look like this:

7v26IM9P2kmVepd7ZxuXyQ==

But as Jeff Atwood states on his site, you can also push a GUID into an ASCII85 encoded string with 20 characters:

[Rb*hlkkXVW+q4s(YSF0

For more inspiration, see: http://www.codinghorror.com/blog/2005/10/equipping-our-ascii-armor.html

4
  • 4
    I posted an Ascii-85 encoder/decoder in C# here: stackoverflow.com/questions/2827627/… Nov 24, 2010 at 13:52
  • 1
    If only this answer had a big, bold "None" at the top to bait readers and act as a TL;DR. ;)
    – jpmc26
    Nov 9, 2017 at 23:22
  • OP specifically asked for Microsoft's implementation. May 16, 2022 at 21:03
  • @ryanwebjackson you are absolutely right of course but I thought this answer might benefit users ending up on this question based purely on the title. May 23, 2022 at 14:12
14

As Adam mentioned from the MSDN quote, UUIDs are 128-bit values. This means that they take 16 bytes of RAM to hold a value. A text representation will take 32 bytes (two bytes for each single byte), plus the 4 hyphens, plus the two brackets if you want to include those; this amounts to 38 bytes.

Just keep in mind that if you are exposing UUIDs to users of your software, they may provide the UUID with or without the brackets. If you're storing the value anywhere, it's best to store it as the 16-byte binary representation. If you are interoperating with other UUID implementations, you may want to use the basic text format for interoperability, since different implementations do different things to the order of bytes when storing a binary UUID value.

0
9

The length depends on the encoding. You can get the standard encoding and length with this snippet:

public void Main() 
{
    var guid = Guid.Empty;
    Write(guid, "N"); // 32 characters
    Write(guid, "D"); // 36 characters (default)
    Write(guid, "B"); // 38 characters
    Write(guid, "P"); // 38 characters
    Write(guid, "X"); // 68 characters
}    

private void Write(Guid guid, string format) 
{
    var guidString = guid.ToString(format);
    Console.WriteLine("{0}: {1} ({2} characters)", format, guidString, guidString.Length);
}

See the Guid.ToString method for details:

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.