Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate GUID from a string that is not in guid format

I would like to generate a GUID from the input string. Let's say I have guid received from the user which is

81a130d2-502f-4cf1-a376-63edeb000e9f

so I can do:

 Guid g = Guid.Parse("81a130d2-502f-4cf1-a376-63edeb000e9f");

which is going to parse successfully.

But how to make user's life easier and allow to input:

81a130d2502f4cf1a37663edeb000e9f

which is without dashes, and still convert it to guid.

If I will try to use the same method it's gonna throw the exception complaining on the missing dashed in the guid format.

Any ideas?

like image 963
inside Avatar asked May 24 '13 17:05

inside


People also ask

How to convert string value to GUID in c#?

The Parse method trims any leading or trailing white space from input and converts the string representation of a GUID to a Guid value. This method can convert strings in any of the five formats produced by the ToString(String) and ToString(String, IFormatProvider) methods, as shown in the following table.

What is GUID format in c#?

A GUID is a 128-bit integer (16 bytes) value. That means that there are more than 300, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000, 000 different values. A big number, isn't it? It is virtually impossible to have duplicates, so it is safe to use.

Is a GUID a string?

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

How many bytes is a GUID?

A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required.


1 Answers

Try

Guid.ParseExact("81a130d2502f4cf1a37663edeb000e9f", "N");
like image 160
Daniel A. White Avatar answered Sep 27 '22 23:09

Daniel A. White