16

GUID you get something like aaaef973-d8ce-4c92-95b4-3635bb2d42d5

Is it always the same? Is it always going to have the following format

8 char "-", 4 char "-", 4 char "-", 4 char "-", 12 char

I'm asking because i need to convert a GUID without "-" to GUID with "-" and vice visa.

001
  • 59,081
  • 87
  • 224
  • 333

3 Answers3

33

No; there are other formats, such as the format you listed except with braces. There's also more complex formats. Here are some of the formats MSDN lists:

UUID formats

  • 32 digits: 00000000000000000000000000000000 (N)
  • 32 digits separated by hyphens: 00000000-0000-0000-0000-000000000000 (D)
  • 32 digits separated by hyphens, enclosed in braces: {00000000-0000-0000-0000-000000000000} (B)
  • 32 digits separated by hyphens, enclosed in parentheses: (00000000-0000-0000-0000-000000000000) (P)
  • Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces: {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}} (X)

MSDN

rjv
  • 5,499
  • 4
  • 28
  • 48
icktoofay
  • 122,243
  • 18
  • 242
  • 228
  • I'm curious _why_ GUIDs are rendered in groups of 8,4,4,4,12 instead of (for example) 4 groups of 8 - and why the `"X"` format has such a strange format. What's so special about the last 8 bytes that requires them to be rendered differently? – Dai Dec 14 '19 at 02:48
  • @Dai: See [UUID format: 8-4-4-4-12 - Why?](https://stackoverflow.com/q/10687505) – icktoofay Apr 25 '20 at 21:47
4

You should simply rely upon it being 32 hexadecimal characters, there can be a variety of ways to present it. Check the Wikipedia article for more information, including a description of how they are commonly written.

For your conversion you should really rely on the static Guid.Parse() methods. Using a mix of your example and the ones in icktoofay's answer, this works nicely:

        var z = Guid.Parse("aaaef973-d8ce-4c92-95b4-3635bb2d42d5");
        z = Guid.Parse("{aaaef973-d8ce-4c92-95b4-3635bb2d42d5}");
        z = Guid.Parse("{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}");

then for outputting them with or without hyphens etc you can use the Guid.ToString() method with one of the established format codes.

Community
  • 1
  • 1
slugster
  • 48,492
  • 14
  • 96
  • 143
0

Most of the time, GUIDS are 32-character hexadecimal strings such as {21EC2020-3AEA-1069-A2DD-08002B30309D} (unless they're encoded in Base-64), and are usually stored as a 128-bit integers. They won't always have hyphens, though.

Liam
  • 25,247
  • 27
  • 110
  • 174
lottscarson
  • 578
  • 5
  • 14