How many characters are there in a GUID?

EncodingGuid

Encoding Problem Overview


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.

Encoding Solutions


Solution 1 - Encoding

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

Solution 2 - Encoding

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

Solution 3 - Encoding

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.

Solution 4 - Encoding

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:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJim CountsView Question on Stackoverflow
Solution 1 - EncodingAdam DavisView Answer on Stackoverflow
Solution 2 - EncodingWiebe TijsmaView Answer on Stackoverflow
Solution 3 - EncodingMichael TrauschView Answer on Stackoverflow
Solution 4 - EncodingMovGP0View Answer on Stackoverflow