Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am generating a GUID using the following statement in my code

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

But, when a GUID is generated, I find that it contains the hyphen character too. How do I go about in generating a GUID with only letters (upper case and lower case) and numbers? I do not want the hyphen. Can someone give me so idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
437 views
Welcome To Ask or Share your Answers For Others

1 Answer

Note that you are talking about the (canonical) string representation of a Guid. The Guid itself is actually a 128-bit integer value.

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

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

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

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

A guid with only either letter or digits makes no sense. A guid string representation is hexadecimal, and thus will always (well most likely) contain both.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...