Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

Have you ever gotten the error:

Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

And your string looks valid?   Case in point:

Guid.Parse(“a2d979b0-26b5-11e7-be4d-cd02cf813brd”)
‘Guid.Parse(“a2d979b0-26b5-11e7-be4d-cd02cf813brd”)’ threw an exception of type ‘System.FormatException’
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233033
HelpLink: null
InnerException: null
Message: “Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).”
Source: “mscorlib”
StackTrace: ” at System.Guid.GuidResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument, String failureArgumentName, Exception innerException)\r\n at System.Guid.TryParseGuidWithDashes(String guidString, GuidResult& result)\r\n at System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult& result)\r\n at System.Guid.Parse(String input)”
TargetSite: {Void SetFailure(ParseFailureKind, System.String, System.Object, System.String, System.Exception)}

So what gives?  The Guid appears valid in format:

—–8—– – —4— – —4— —4— ——–12——–

a2d979b0 – 26b5 – 11e7 – be4d – cd02cf813brd

If you look at the definition of the Guid type in .NET:

https://msdn.microsoft.com/en-us/library/96ff78dc(v=vs.110).aspx

Type: System.String

A string that contains a GUID in one of the following formats (“d” represents a hexadecimal digit whose case is ignored):

32 contiguous digits:

dddddddddddddddddddddddddddddddd

-or-

Groups of 8, 4, 4, 4, and 12 digits with hyphens between the groups. The entire GUID can optionally be enclosed in matching braces or parentheses:

dddddddd-dddd-dddd-dddd-dddddddddddd

-or-

{dddddddd-dddd-dddd-dddd-dddddddddddd}

-or-

(dddddddd-dddd-dddd-dddd-dddddddddddd)

-or-

Groups of 8, 4, and 4 digits, and a subset of eight groups of 2 digits, with each group prefixed by “0x” or “0X”, and separated by commas. The entire GUID, as well as the subset, is enclosed in matching braces:

{0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}

All braces, commas, and “0x” prefixes are required. All embedded spaces are ignored. All leading zeros in a group are ignored.

The digits shown in a group are the maximum number of meaningful digits that can appear in that group. You can specify from 1 to the number of digits shown for a group. The specified digits are assumed to be the low-order digits of the group.

So, whats wrong with my string?

a2d979b0-26b5-11e7-be4d-cd02cf813brd

A GUID is a 16-byte (128-bit) number, typically represented by a 32-character hexadecimal string. A GUID (in hex form) need not contain any alpha characters, though by chance it probably would. If you are targeting a GUID in hex form, you can check that the string is 32-characters long (after stripping dashes and curly brackets) and has only letters A-F and numbers.

So, there is the problem, a Guid isn’t just a random string of Digits and Characters, it’s a string representation of a 128 bit number!

Make sure you only have A-F and 0-9!

What else can go wrong?  Check out this Guid:

“E6F85AE0‐F479‐4E98‐9287‐98F7E62BA083”

When parsing it I get:

Guid.Parse(“E6F85AE0‐F479‐4E98‐9287‐98F7E62BA083”)
‘Guid.Parse(“E6F85AE0‐F479‐4E98‐9287‐98F7E62BA083”)’ threw an exception of type ‘System.FormatException’
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233033
HelpLink: null
InnerException: null
Message: “Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).”
Source: “mscorlib”
StackTrace: ” at System.Guid.TryParseGuidWithNoStyle(String guidString, GuidResult& result)\r\n at System.Guid.TryParseGuid(String g, GuidStyles flags, GuidResult& result)\r\n at System.Guid.Parse(String input)”
TargetSite: {Boolean TryParseGuidWithNoStyle(System.String, GuidResult ByRef)}

The Guid Seems ok,

“E6F85AE0‐F479‐4E98‐9287‐98F7E62BA083”

All the characters are A-F and 0-9, what else could be wrong?

Try converting your string to its ASCII equivalent:

http://onlinecalculators.brainmeasures.com/Conversions/StringtoAsciiCalculator.aspx

The result:

ASCII: 34 69 54 70 56 53 65 69 48 NULL 70 52 55 57 NULL 52 69 57 56 NULL 57 50 56 55 NULL 57 56 70 55 69 54 50 66 65 48 56 51 34

What are those NULLS?

Look at the HEX:

22 45 36 46 38 35 41 45 30 1 46 34 37 39 1 34 45 39 38 1 39 32 38 37 1 39 38 46 37 45 36 32 42 41 30 38 33 22

Retype the string manually (DONT COPY/PASTE!)

“E6F85AE0-F479-4E98-9287-98F7E62BA083”

Now get its ASCII

34 69 54 70 56 53 65 69 48 45 70 52 55 57 45 52 69 57 56 45 57 50 56 55 45 57 56 70 55 69 54 50 66 65 48 56 51 34

(No more NULLS)

and HEX:

22 45 36 46 38 35 41 45 30 2D 46 34 37 39 2D 34 45 39 38 2D 39 32 38 37 2D 39 38 46 37 45 36 32 42 41 30 38 33 22

See the difference?

22 45 36 46 38 35 41 45 30 1 46 34 37 39 1 34 45 39 38 1 39 32 38 37 1 39 38 46 37 45 36 32 42 41 30 38 33 22 

22 45 36 46 38 35 41 45 30 2D 46 34 37 39 2D 34 45 39 38 2D 39 32 38 37 2D 39 38 46 37 45 36 32 42 41 30 38 33 22

Its the hyphens!  They are actually a different character!

This is a common issue on the web!  Sometimes when encoding values from Unicode, ASCII, UTF-8, etc, characters can get transposed.

So, next time you run into some “weirdness” with strings and Guids, Don’t give up! think outside the box!

One thought on “Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).”

Leave a Reply to Jagjeet Cancel reply

Your email address will not be published. Required fields are marked *