c# 将文件gbk转换为utf-8
2个回答
展开全部
gb2312--utf-8的,原理一样换一下就可以
private string GB2312ToUTF8(string str)
{
try
{
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("GB2312");
byte[] unicodeBytes = gb2312.GetBytes(str);
byte[] asciiBytes = Encoding.Convert(gb2312, utf8, unicodeBytes);
char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string result = new string(asciiChars);
return result;
}
catch
{
return "";
}
}
private string GB2312ToUTF8(string str)
{
try
{
Encoding utf8 = Encoding.UTF8;
Encoding gb2312 = Encoding.GetEncoding("GB2312");
byte[] unicodeBytes = gb2312.GetBytes(str);
byte[] asciiBytes = Encoding.Convert(gb2312, utf8, unicodeBytes);
char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string result = new string(asciiChars);
return result;
}
catch
{
return "";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询