第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何對(duì)base 64字符串進(jìn)行編碼和解碼?

如何對(duì)base 64字符串進(jìn)行編碼和解碼?

C#
元芳怎么了 2019-06-27 16:15:18
如何對(duì)base 64字符串進(jìn)行編碼和解碼?如何返回給定字符串的base 64編碼字符串?如何將base 64編碼的字符串解碼為字符串?
查看完整描述

3 回答

?
幕布斯6054654

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超7個(gè)贊

我正在與一些簡(jiǎn)潔的特性分享我的實(shí)現(xiàn):

  • 使用擴(kuò)展方法對(duì)類(lèi)進(jìn)行編碼。理由是某人可能需要支持不同類(lèi)型的編碼(不僅僅是UTF 8)。
  • 另一個(gè)改進(jìn)是在空項(xiàng)的空結(jié)果方面失敗-它在現(xiàn)實(shí)生活中非常有用,并且支持X=decode(encode(X)的等價(jià)性。

備注:要使用擴(kuò)展方法,請(qǐng)記住不得不(!)使用using關(guān)鍵字(在本例中)using MyApplication.Helpers.Encoding).

代碼:

namespace MyApplication.Helpers.Encoding{
    public static class EncodingForBase64
    {
        public static string EncodeBase64(this System.Text.Encoding encoding, string text)
        {
            if (text == null)
            {
                return null;
            }

            byte[] textAsBytes = encoding.GetBytes(text);
            return System.Convert.ToBase64String(textAsBytes);
        }

        public static string DecodeBase64(this System.Text.Encoding encoding, string encodedText)
        {
            if (encodedText == null)
            {
                return null;
            }

            byte[] textAsBytes = System.Convert.FromBase64String(encodedText);
            return encoding.GetString(textAsBytes);
        }
    }}

用法示例:

using MyApplication.Helpers.Encoding; // !!!namespace ConsoleApplication1{
    class Program
    {
        static void Main(string[] args)
        {
            Test1();
            Test2();
        }

        static void Test1()
        {
            string textEncoded = System.Text.Encoding.UTF8.EncodeBase64("test1...");
            System.Diagnostics.Debug.Assert(textEncoded == "dGVzdDEuLi4=");

            string textDecoded = System.Text.Encoding.UTF8.DecodeBase64(textEncoded);
            System.Diagnostics.Debug.Assert(textDecoded == "test1...");
        }

        static void Test2()
        {
            string textEncoded = System.Text.Encoding.UTF8.EncodeBase64(null);
            System.Diagnostics.Debug.Assert(textEncoded == null);

            string textDecoded = System.Text.Encoding.UTF8.DecodeBase64(textEncoded);
            System.Diagnostics.Debug.Assert(textDecoded == null);
        }
    }}


查看完整回答
反對(duì) 回復(fù) 2019-06-27
  • 3 回答
  • 0 關(guān)注
  • 654 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)