tags:

views:

2279

answers:

5

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.

+20  A: 

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 Davis
Actually, it does. What part of the question is left unanswered?
Beska
See phsr's answer.
magnifico
His answer is the same...
Giovanni Galbo
*sigh* Why bother teaching a person to fish when you can just hand him one, and have him ask more questions here later?
Beska
@magnifico -- see the last paragraph of this answer: "So a total of 38 characters in the typical hexadecimal encoding with curly braces."
Randolpho
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
That was after an edit. So I will withdraw my down vote.
magnifico
+6  A: 
Guid.NewGuid().ToString("B").Length
casperOne
Doesn't answer the question. It would if you would post the output of that code.
magnifico
@Magnifico: You've got to be kidding. Do you really think that answers like "38" are more useful than "here is how you can discover the answer to this problem and things like it in the future"?
Beska
Yes, not everyone who uses GUIDs uses .NET, so not everyone can run the code sample.
magnifico
But those people are also less likely to care about the "Microsoft specific" convention for displaying them, too.
Beska
+3  A: 

38, counting braces and dashes

Adam Davis's answer is far superior

phsr
+3  A: 

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 brakets if you want 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-bit 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.

Michael Trausch
+2  A: 

Sorry if it's a bit off topic, and sorry for digging up this old issue, but I stumbled upon this thread while looking for an answer similar to this:

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

Zidad