本文介绍了如何将iso-8859-1正确转换为utf8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将响应流写入文件.流包含一个编码页面(iso-8859-1).
I need to write a response stream to a file. The stream contains an encoded page (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;
}
问题是decode()无法正常工作
The problem is that decode() is not working
如何将iso-8859-1正确转换为utf8?
How can I convert iso-8859-1 to utf8 correctly?
更新
我依靠提琴手来获取内容类型:
I rely on fiddler to get the content-type:
推荐答案
鉴于这种情况,它应该足以:
Given the situation it should be enough to:
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);
}
}
}
如果这不起作用,请检查您的数据. (第一行)在调试器中的外观如何?
If this doesn't work, check your data. How does the (first) line look in the debugger?
这篇关于如何将iso-8859-1正确转换为utf8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!