是否有可能或者是否有任何重载来获得少于32个字符的GUID?目前我正在使用这个声明,但它给了我错误
string guid = new Guid("{dddd-dddd-dddd-dddd}").ToString();
Run Code Online (Sandbox Code Playgroud)
我想要一个20个字符的密钥
在 URL 或最终用户可见的其他地方使用 ShortGuids 很好。
以下代码:
Guid guid = Guid.NewGuid();
ShortGuid sguid1 = guid; // implicitly cast the guid as a shortguid
Console.WriteLine( sguid1 );
Console.WriteLine( sguid1.Guid );
Run Code Online (Sandbox Code Playgroud)
会给你这个输出:
FEx1sZbSD0ugmgMAF_RGHw
b1754c14-d296-4b0f-a09a-030017f4461f
Run Code Online (Sandbox Code Playgroud)
这是编码和解码方法的代码:
public static string Encode(Guid guid)
{
string encoded = Convert.ToBase64String(guid.ToByteArray());
encoded = encoded
.Replace("/", "_")
.Replace("+", "-");
return encoded.Substring(0, 22);
}
public static Guid Decode(string value)
{
value = value
.Replace("_", "/")
.Replace("-", "+");
byte[] buffer = Convert.FromBase64String(value + "==");
return new Guid(buffer);
}
Run Code Online (Sandbox Code Playgroud)