C# 使用base64对字符串进行编码和解码

需要引入命名空间:

using System;
using System.Text;


解码:

    public static string UnBase64String(string value)
    {
        if (value == null || value == "")
        {
            return "";
        }
        byte[] bytes = Convert.FromBase64String(value);
        return Encoding.UTF8.GetString(bytes);
    }


编码:

    public static string ToBase64String(string value)
    {
        if (value == null || value == "")
        {
            return "";
        }
        byte[] bytes = Encoding.UTF8.GetBytes(value);
        return Convert.ToBase64String(bytes);
    }
测试:

	public static void Base64Test(){
		string base64string = GameCommon.ToBase64String("aaaa11233Base64编码和解码");
		
		string unbase64string = GameCommon.UnBase64String(base64string);
		
		Debug.Log("base64string : " + base64string);
		Debug.Log("unbase64string : " + unbase64string);
	}
结果:



你可能感兴趣的:(c#,unity)