C# StreamWriter 中的 Windows-1252 编码返回 ANSI 编码文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13255733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:57:12  来源:igfitidea点击:

Windows-1252 Encoding in StreamWriter return ANSI encoded file

c#stringc#-4.0

提问by Maxime

I try to encode a string in Windows-1252 with a StreamWriter. The input string (dataString) is encode in UTF8.

我尝试使用 StreamWriter 在 Windows-1252 中对字符串进行编码。输入字符串 (dataString) 以 UTF8 编码。

StreamWriter sw = new StreamWriter(@"C:\Temp\data.txt", true, Encoding.GetEncoding(1252));
sw.Write(dataString);
sw.Close();

When I open the file in Notepad++ I get a ANSI file. I need a Windows-1252 encoded file.

当我在 Notepad++ 中打开文件时,我得到一个 ANSI 文件。我需要一个 Windows-1252 编码的文件。

Someone have an idea?

有人有想法吗?

采纳答案by shf301

Your file is Windows-1252 encoded. There is no data in the file of a non-Unicode to indicate how the file is encoded. In this case ANSI just means not Unicode. If you where to encode the as Russian/Windows-1251 and open it in Notepad++, Notepad++ would display it as ANSI as well.

您的文件是 Windows-1252 编码的。非 Unicode 的文件中没有数据表明文件是如何编码的。在这种情况下,ANSI 只是意味着不是 Unicode。如果您将其编码为俄语/Windows-1251 并在 Notepad++ 中打开它,Notepad++ 也会将其显示为 ANSI。

See Unicode, UTF, ASCII, ANSI format differencesfor more info.

有关详细信息请参阅Unicode、UTF、ASCII、ANSI 格式差异