使用 C# 将文本文件从 ANSI 转换为 ASCII

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/733871/
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-04 23:05:03  来源:igfitidea点击:

Converting text file from ANSI to ASCII using C#

c#encodingcharacter-encodingascii

提问by BDeveloper

I have an ANSI-encoded file, and I want to convert the lines I read from the file to ASCII.

我有一个 ANSI 编码的文件,我想将从文件中读取的行转换为 ASCII。

How do I go about doing this in C#?

我如何在 C# 中做到这一点?



EDIT :What if i used "BinaryReader" BinaryReader reader = new BinaryReader(input, Encoding.Default);but this reader takes (Stream, Encoding) but "Stream" is an abstract! And where should I put the path of the file which he will read from ?

编辑:如果我使用“BinaryReader”BinaryReader reader = new BinaryReader(input, Encoding.Default);但这个阅读器采用(流,编码)但“流”是一个抽象怎么办 !我应该在哪里放置他将从中读取的文件的路径?

采纳答案by Can Berk Güder

A direct conversion from ANSI to ASCII might not always be possible, since ANSI is a superset of ASCII.

从 ANSI 到 ASCII 的直接转换可能并不总是可行,因为 ANSI 是 ASCII 的超集。

You can try converting to UTF-8 using Encoding, though:

不过,您可以尝试使用 转换为 UTF-8 Encoding

Encoding ANSI = Encoding.GetEncoding(1252);

byte[] ansiBytes = ANSI.GetBytes(str);
byte[] utf8Bytes = Encoding.Convert(ANSI, Encoding.UTF8, ansiBytes);

String utf8String = Encoding.UTF8.GetString(utf8Bytes);

Of course you can replace UTF8 with ASCII, but that doesn't really make sense since:

当然,您可以用 ASCII 替换 UTF8,但这实际上没有意义,因为:

  • if the original string doesn't contain any byte > 126, then it's already ASCII
  • if the original string does contain one or more bytes > 126, then those bytes will be lost
  • 如果原始字符串不包含任何大于 126 的字节,则它已经是 ASCII
  • 如果原始字符串确实包含一个或多个大于 126 的字节,则这些字节将丢失

UPDATE:

更新:

In response to the updated question, you can use BinaryReaderlike this:

针对更新后的问题,您可以这样使用BinaryReader

BinaryReader reader = new BinaryReader(File.Open("foo.txt", FileMode.Open),
                                       Encoding.GetEncoding(1252));

回答by Marc Gravell

Basically, you need to specify an Encodingwhen reading/writing the file. For example:

基本上,您需要Encoding在读取/写入文件时指定一个。例如:

// read with the **local** system default ANSI page
string text = File.ReadAllText(path, Encoding.Default); 

// ** I'm not sure you need to do this next bit - it sounds like
//  you just want to read it? **

// write as ASCII (if you want to do this)
File.WriteAllText(path2, text, Encoding.ASCII);

Note that once you have read it, textis actually unicode when in memory.

请注意,一旦您阅读了它,text实际上是在内存中的 unicode。

You can choose different code-pages using Encoding.GetEncoding.

您可以使用 选择不同的代码页Encoding.GetEncoding