19

I need to format my GUIDs in the dashed format, all uppercase. I know using myGuid.ToString("D") or String.Format("{0:D}", myGuid) gives the dashed format, but using an uppercase D as opposed to a lower-case d doesn't give me an uppercased GUID like I thought it would. Is there a way to do this without doing anything crazy, or do I just need to call myGuid.ToString().ToUpper()?

4 Answers 4

23

Note that RFC 4122, which defines the UUID specification, stipulates that output hex characters should be in lowercase when converting the structure to a string:

  The hexadecimal values "a" through "f" are output as
  lower case characters and are case insensitive on input.

This may explain why the Guid structure does not support outputting directly as an uppercase string.

Since the ToString format provider parameter is ignored, the only alternative (without simply converting the string to uppercase) would be to directly manipulate the bytes, while taking care to preserve endianness. Simply converting to uppercase (either directly or through an extension method) is probably far more straightforward.

2
  • 4
    The international spec of 2008-08, itu.int/rec/T-REC-X.667/en, agrees with that RFC 4122: 6.5.4 Software generating the hexadecimal representation of a UUID shall not use upper case letters. NOTE – It is recommended that the hexadecimal representation used in all human-readable formats be restricted to lower-case letters. Software processing this representation is, however, required to accept both upper and lower case letters as specified in 6.5.2. May 8, 2013 at 23:41
  • 2
    I know this is some what old by now but I find it interesting that the "Create GUID" tool in Visual Studio generates uppercase GUID's. Also the WIX toolset recommends that GUID's should be uppercase for complete support. I can't remember the specifics of that recommandation I just remember reading it. Jun 29, 2015 at 9:18
22

do I just need to call myGuid.ToString().ToUpper()

Yep. You could go to the effort of creating a custom IFormatProvider, but it doesn't seem worth it here.

2

I don't think you have any other choice than to just do myGuid.ToString().ToUpper(). Although, you could always write an extension method, perhaps something like ToUpperString, but I don't think there is anything built into the system.

1

Assuming you have a class that holds your Guid and you would like to retain the typed Guid, you could do something like this:

public Guid Identifier { get; set; }

public String FormattedIdentifier => Identifier.ToString().ToUpper();
1
  • I think it still provides some value. My example shows how you might encapsulate the formatting of the Guid. Mar 22, 2013 at 2:01

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.