asp.net 转换GB2312 的字符串为UTF8编码

 /// <summary>
    /// 调用函数
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(GB2312ToUTF8("大家好,欢迎访问 http://www.my400800.cn "));

    }

    /// <summary>
    /// 转换GB2312 的字符串为UTF8编码
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public string GB2312ToUTF8(string str)
    {
        try
        {
            Encoding uft8 = Encoding.GetEncoding(65001);
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            byte[] temp = gb2312.GetBytes(str);
            Response.Write("gb2312的编码的字节个数:" + temp.Length+"<br>");
            for (int i = 0; i < temp.Length; i++)
            {
                Response.Write(Convert.ToUInt16(temp[i]).ToString() + "<br>");
            }
            byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);
            Response.Write("uft8的编码的字节个数:" + temp1.Length + "<br>");
            for (int i = 0; i < temp1.Length; i++)
            {
                Response.Write(Convert.ToUInt16(temp1[i]).ToString() + "<br>");
            }
            string result = uft8.GetString(temp1);
            return result;
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
            return null;
        }
    }

原文地址:https://www.cnblogs.com/jishu/p/1940111.html