问题
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.
回答1:
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
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/7775439/is-the-format-of-guid-always-the-same