Skip to content

Instantly share code, notes, and snippets.

@DamianReeves
Created November 11, 2015 17:01
Show Gist options
  • Save DamianReeves/a0bfe0105d0376e7002f to your computer and use it in GitHub Desktop.
Save DamianReeves/a0bfe0105d0376e7002f to your computer and use it in GitHub Desktop.
// credit: http://madskristensen.net/post/A-shorter-and-URL-friendly-GUID
using System;
public static class GuidEncoder
{
public static string Encode(string guidText)
{
Guid guid = new Guid(guidText);
return Encode(guid);
}
public static string Encode(Guid guid)
{
string enc = Convert.ToBase64String(guid.ToByteArray());
enc = enc.Replace("/", "_");
enc = enc.Replace("+", "-");
return enc.Substring(0, 22);
}
public static Guid Decode(string encoded)
{
encoded = encoded.Replace("_", "/");
encoded = encoded.Replace("-", "+");
byte[] buffer = Convert.FromBase64String(encoded + "==");
return new Guid(buffer);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment