Skip to content

Instantly share code, notes, and snippets.

@GioviQ
Created January 26, 2021 13:57
Show Gist options
  • Save GioviQ/e820fc13d18f93b27dfb48275d7b0d02 to your computer and use it in GitHub Desktop.
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
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