How to retain similar character encoding
Posted
by Mystere Man
on Stack Overflow
See other posts from Stack Overflow
or by Mystere Man
Published on 2010-05-27T19:13:37Z
Indexed on
2010/05/27
19:31 UTC
Read the original article
Hit count: 233
I have a logfile that contains the half character ½, I need to process this log file and rewrite certain lines to a new file, which contain that character. However, when I write out the file the characters appear in notepad incorrectly.
I know this is some kind of encoding issue, and i'm not sure if it's just that the files i'm writing don't contain the correct bom or what.
I've tried reading and writing the file with all the available encoding options in the Encoding enumeration.
I'm using this code:
string line;
// Note i've used every version of the Encoding enumeration
using (StreamReader sr = new StreamReader(file, Encoding.Unicode))
using (StreamWRiter sw = new StreamWriter(newfile, false, Encoding.Unicode))
{
while ((line = sr.ReadLine()) != null)
{
// process code, I do not alter the lines, they are copied verbatim
// but i do not write every line that i read.
sw.WriteLine(line);
}
}
When I view the original log in notepad, the half character displays correctly. When I view the new file, it does not. Can anyone help me to solve this?
© Stack Overflow or respective owner