如何将iso-8859-1正确转换为utf8?
作者:互联网
我需要将响应流写入文件.流包含一个编码页面(iso-8859-1).
这是我的代码:
...
using (TextWriter writer = new StreamWriter(tmpFilePath))
{
using (TextReader reader = new StreamReader(answer, Encoding.GetEncoding("ISO-8859-1")))
{
string line = "";
while ((line = reader.ReadLine()) != null)
{
// try to decode
string decoded_line = decode(line);
writer.WriteLine(decoded_line);
}
}
}
...
string decode(string message)
{
string result = "";
Encoding iso = Encoding.GetEncoding("iso-8859-1");
Encoding utf8 = Encoding.UTF8;
byte[] isoBytes = iso.GetBytes(message);
byte[] utf8Bytes = Encoding.Convert(iso, utf8, isoBytes);
result = utf8.GetString(utf8Bytes);
return result;
}
问题是解码()不起作用
如何将iso-8859-1正确转换为utf8?
更新
我依靠提琴手来获取内容类型:
解决方法:
考虑到这种情况,应该足以:
using (TextWriter writer = new StreamWriter(tmpFilePath, Ecoding.UTF8))
{
using (TextReader reader = new StreamReader(answer, Encoding.GetEncoding("ISO-8859-1")))
{
while ((line = reader.ReadLine()) != null)
{
writer.WriteLine(decoded_line);
}
}
}
如果这不起作用,请检查您的数据. (第一行)在调试器中的外观如何?
标签:iso-8859-1,encoding,utf-8,c 来源: https://codeday.me/bug/20191101/1984966.html