Created
May 14, 2016 11:52
-
-
Save NickCraver/6eea63a6f443d94ff26b7391dc8ddf90 to your computer and use it in GitHub Desktop.
Base64 Header Example
This file contains 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
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