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

前端 未结 6 770
清歌不尽
清歌不尽 2021-02-13 11:25

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

Thanks!

6条回答
  •  长情又很酷
    2021-02-13 12:16

    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);
    

提交回复
热议问题