Created
November 11, 2015 17:01
-
-
Save DamianReeves/a0bfe0105d0376e7002f to your computer and use it in GitHub Desktop.
GuidEncoder from http://madskristensen.net/post/A-shorter-and-URL-friendly-GUID
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
// 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