C#编码转换

2021-01-23 17:14

阅读:558

标签:returns   summary   字符   ram   byte   encoding   声明   ret   utf8   

///


/// GB2312转换成UTF8
///

///
///
public static string gb2312_utf8(string text)
{
//声明字符集
System.Text.Encoding utf8, gb2312;
//gb2312
gb2312 = System.Text.Encoding.GetEncoding("gb2312");
//utf8
utf8 = System.Text.Encoding.GetEncoding("utf-8");
byte[] gb;
gb = gb2312.GetBytes(text);
gb = System.Text.Encoding.Convert(gb2312, utf8, gb);
//返回转换后的字符
return utf8.GetString(gb);
}

    /// 
    /// UTF8转换成GB2312
    /// 
    /// 
    /// 
    public static string utf8_gb2312(string text)
    {
        //声明字符集   
        System.Text.Encoding utf8, gb2312;
        //utf8   
        utf8 = System.Text.Encoding.GetEncoding("utf-8");
        //gb2312   
        gb2312 = System.Text.Encoding.GetEncoding("gb2312");
        byte[] utf;
        utf = utf8.GetBytes(text);
        utf = System.Text.Encoding.Convert(utf8, gb2312, utf);
        //返回转换后的字符   
        return gb2312.GetString(utf);
    }

C#编码转换

标签:returns   summary   字符   ram   byte   encoding   声明   ret   utf8   

原文地址:https://www.cnblogs.com/LCLBook/p/12073836.html


评论


亲,登录后才可以留言!