Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET GUID uppercase string format

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()?

like image 451
Daniel Schaffer Avatar asked Jul 29 '11 03:07

Daniel Schaffer


People also ask

Can GUID be uppercase?

Yes, GUIDs are case-sensitive.

What format is GUID?

The GUID data type is a text string representing a Class identifier (ID). COM must be able to convert the string to a valid Class ID. All GUIDs must be authored in uppercase. The valid format for a GUID is {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} where X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).

Is GUID a string?

A GUID is a 16-byte (128-bit) number, typically represented by a 32-character hexadecimal string.


1 Answers

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.

like image 113
drf Avatar answered Sep 30 '22 06:09

drf