Created
January 26, 2021 13:57
-
-
Save GioviQ/e820fc13d18f93b27dfb48275d7b0d02 to your computer and use it in GitHub Desktop.
GuidUtil with extension to compress Guid string representation useful for Guid as url parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class GuidUtil | |
{ | |
public static string ToCompressedString(this Guid guid) | |
{ | |
return Convert.ToBase64String(guid.ToByteArray()) | |
.Substring(0, 22) | |
.Replace('+', '-') | |
.Replace('/', '_'); | |
} | |
public static Guid FromCompressedString(string compressedGuid) | |
{ | |
string base64 = compressedGuid | |
.Replace('_', '/') | |
.Replace('-', '+') | |
+ "=="; | |
return new Guid(Convert.FromBase64String(base64)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment