nuget包
安装System.Text.Encoding.CodePages
使用
//将byte[]转化为gb2312的字符串,要确保byte[]是存储的gb2312的字符串,要不然会乱码
string ToGb213(byte[] str)
{
//首先需要注册
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding gb2312;
gb2312 = Encoding.GetEncoding("GB2312");
return gb2312.GetString(str);
}
//utf-8编码的字符串转化为gb2312编码的字符串
string LanChange(string str)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Encoding utf8;
Encoding gb2312;
utf8 = Encoding.GetEncoding("UTF-8");
gb2312 = Encoding.GetEncoding("GB2312");
byte[] gb = gb2312.GetBytes(str);
gb = Encoding.Convert(gb2312, utf8, gb);
return utf8.GetString(gb);
}