How can I convert iso-8859-1 to utf8 correctly?

老子叫甜甜 提交于 2019-12-10 21:10:14

问题


I need to write a response stream to a file. The stream contains an encoded page (iso-8859-1).

This is my code:

...

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;
}

The problem is that decode() is not working

How can I convert iso-8859-1 to utf8 correctly?

UPDATE

I rely on fiddler to get the content-type:


回答1:


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?




回答2:


You're decoding twice - you already specified the encoding when creating the StreamReader. The resulting string should already be properly decoded.

This doesn't explain why message looks like that - it should look like an ordinary string. Are you sure the file you're reading contains what you think it contains?



来源:https://stackoverflow.com/questions/9540936/how-can-i-convert-iso-8859-1-to-utf8-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!