Base64 和 字符串的相互转换

1、Base64转换为字符串

 1 /// <summary>
 2         /// base64转为字符串
 3         /// </summary>
 4         /// <param name="str"></param>
 5         /// <returns></returns>
 6         public string Base64ToString(string strBase)
 7         {
 8             byte[] bs = Convert.FromBase64String(strBase);
 9             var str = System.Text.Encoding.UTF8.GetString(bs);
10             return str.ToString();
11         }
12 
13        //
14          var str = Encoding.UTF8.GetString(Convert.FromBase64String(data));  

2、字符串转换为Base64

/// <summary>
        /// 字符串转为base64
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string ToBase64(string str)
        {
            var byteStr = Encoding.UTF8.GetBytes(str);
            var bs = Convert.ToBase64String(byteStr);
            return bs.ToString();
        }

        //
            var strBase = Convert.ToBase64String(Encoding.UTF8.GetBytes(str));

 

posted @ 2022-07-07 13:34  进击的黑大帅  阅读(4281)  评论(0编辑  收藏  举报