Hyphenated GUID Generation

I generate a GUID using the following statement in my code

byte[ ] keyBytes = Encoding.UTF8.GetBytes( Guid.NewGuid( ).ToString( ).Substring( 0, 12 ) ); 

But, when the GUID is generated, I found that it also contains a hyphen. How can I generate a GUID with only letters (uppercase and lowercase) and numbers? I do not want a hyphen. Can anyone give me such an idea?

+68
c # winforms
Jan 16 '12 at 8:45
source share
2 answers

Note that you are talking about the (canonical) string representation of Guid. In fact, Guid is a 128-bit integer value.

You can use the "N" Guid.ToString(String) with an overload of Guid.ToString(String) .

 Guid.NewGuid().ToString("N"); 

By default, lowercase letters. A manual with only capital letters can only be achieved by manually converting them all to uppercase, for example:

 Guid.NewGuid().ToString("N").ToUpper(); 

A pointer with a letter or numbers does not make sense. The guideline representation is hexadecimal and therefore always (well, most likely) contains both.

+201
Jan 16 2018-12-12T00:
source share
 Guid.NewGuid().ToString().Replace("-", string.Empty) 
0
Jan 16 2018-12-12T00:
source share



All Articles