问题
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