开发者

How to retain similar character encoding

开发者 https://www.devze.com 2023-01-01 21:17 出处:网络
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 chara

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. This tells me the problem is not with notepad being able to display the character, because it works in the o开发者_如何学运维riginal.

Can anyone help me to solve this?


The solution was PEBKAC.

I was changing the encodings in a different part of the program that wasn't creating these files. When I changed the correct files, using Encoding.Default, it displays correctly.

Thanks Jon and others.

0

精彩评论

暂无评论...
验证码 换一张
取 消