It is usually simple to replace the alphabet for use in URLs, so no% coding is required; only 3 out of 65 characters are problematic - + , / and = . the most common replacements are - instead of + and _ instead of / . As for the add-on: just delete it ( = ); You can specify the amount of filling required. At the other end: just change the process:
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes) .TrimEnd(padding).Replace('+', '-').Replace('/', '_');
from:
static readonly char[] padding = { '=' };
and vice versa:
string incoming = returnValue .Replace('_', '/').Replace('-', '+'); switch(returnValue.Length % 4) { case 2: incoming += "=="; break; case 3: incoming += "="; break; } byte[] bytes = Convert.FromBase64String(incoming); string originalText = Encoding.ASCII.GetString(bytes);
An interesting question, however, is this: is the same approach used by the “common codec library”? Of course, it would be wise to check first - this is a fairly common approach.
Marc Gravell Oct. 14 '14 at 7:10 2014-10-14 07:10
source share