Skip to content

Instantly share code, notes, and snippets.

@duncansmart
Created October 30, 2012 20:45
Show Gist options
  • Save duncansmart/3982912 to your computer and use it in GitHub Desktop.
Save duncansmart/3982912 to your computer and use it in GitHub Desktop.
Guid compression
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);
}
Copy link

ghost commented Jan 16, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment