Skip to content

Instantly share code, notes, and snippets.

@NickCraver
Created May 14, 2016 11:52
Show Gist options
  • Save NickCraver/6eea63a6f443d94ff26b7391dc8ddf90 to your computer and use it in GitHub Desktop.
Save NickCraver/6eea63a6f443d94ff26b7391dc8ddf90 to your computer and use it in GitHub Desktop.
Base64 Header Example
void Main()
{
"99999999".Dump();
Int32ToBase64(99999999).Dump();
}
private static readonly char[] EqualsChar = { '=' };
private static string Int32ToBase64(int i)
{
try
{
var raw = BitConverter.GetBytes(i);
if (BitConverter.IsLittleEndian) Array.Reverse(raw);
int offset = 0;
while (offset < 3 && raw[offset] == 0) { offset++; }
return Convert.ToBase64String(raw, offset, 4 - offset).TrimEnd(EqualsChar);
}
catch
{
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment