如何保留相似的字符编码

发布于 2024-09-03 22:29:09 字数 758 浏览 2 评论 0原文

我有一个包含半个字符 ½ 的日志文件,我需要处理该日志文件并将某些行重写到包含该字符的新文件中。但是,当我写出文件时,记事本中的字符显示不正确。

我知道这是某种编码问题,我不确定这是否只是我正在编写的文件不包含正确的 bom 或者什么。

我尝试使用 Encoding 枚举中的所有可用编码选项读取和写入文件。

我正在使用这段代码:

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

当我在记事本中查看原始日志时,半个字符显示正确。当我查看新文件时,它没有。这告诉我问题不在于记事本能够显示该字符,因为它在原始版本中工作。

谁能帮我解决这个问题吗?

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 original.

Can anyone help me to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2024-09-10 22:29:09

解决方案是 PEBKAC。

我正在更改未创建这些文件的程序的不同部分中的编码。当我使用 Encoding.Default 更改正确的文件时,它会正确显示。

感谢乔恩和其他人。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文