Created
October 30, 2012 20:45
-
-
Save duncansmart/3982912 to your computer and use it in GitHub Desktop.
Guid compression
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
static string compressGuid(Guid guid) | |
{ | |
// "MspzuC2oLkKjoUEgZrbJFg==" | |
return Convert.ToBase64String(guid.ToByteArray()) | |
.Substring(0, 22) | |
.Replace('+', '-') | |
.Replace('/', '_'); | |
} | |
static Guid expandGuid(string compressedGuid) | |
{ | |
string base64 = compressedGuid | |
.Replace('_', '/') | |
.Replace('-', '+') | |
+ "=="; | |
var bytes = Convert.FromBase64String(base64); | |
return new Guid(bytes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Agree, @trejkaz , however I believe compressing Guid string representations could be useful for more space-effective tagging of transactions/requests in logs (where GUID woud be represented with it's ToString() result instead of 8 bytes in memory). Frankly, I don't think this can be done more efficiently, because it's already raw bytes represented with human-readable characters, because it's not Guid's ToString being converted, but instead it's ToByteArray().
Perhaps, smaz could do a better job. I never tried it though.
Or, use more of the alphabet! However, it definitely defeats the purpose, because the more of the alphabet is used, the less distinguishable the end-result becomes.