generating GUID without hyphen

前端 未结 2 934
臣服心动
臣服心动 2020-12-13 11:45

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

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


        
相关标签:
2条回答
  • 2020-12-13 12:15

    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.

    0 讨论(0)
  • 2020-12-13 12:22
    Guid.NewGuid().ToString().Replace("-", string.Empty)
    
    0 讨论(0)
提交回复
热议问题