C# Method like Base64String, but only alphanumeric (no plus or slash)

Alex picture Alex · Jun 1, 2009 · Viewed 10.6k times · Source

is there any C# method that works similar to Convert.ToBase64String but doesn't generate anything except alphanumeric output?

Thanks!

Answer

Mason G. Zhwiti picture Mason G. Zhwiti · Mar 2, 2013

The answers are a bit outdated now. For the benefit of future searchers: The best way to handle this now in C# is:

byte[] b; // fill your byte array somehow
string s = System.Web.HttpServerUtility.UrlTokenEncode(b);

This returns a Base64-encoded string that is URL-safe (which is what you said you were really after in the comments to your question).

You can then decode it again using, you guessed it:

byte[] b = System.Web.HttpServerUtility.UrlTokenDecode(s);