Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting text file from ANSI to ASCII using C#

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

How do I go about doing this in 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 ?

like image 491
BDeveloper Avatar asked Apr 09 '09 11:04

BDeveloper


People also ask

How do I convert a text file to ascii?

Open the document to export. Click File > Export. In the Export dialog box, click ASCII Text in the Save as type list. In the File Name box, either create a new file by typing a name and extension, or replace data in an existing file with the exported data by selecting the file.

How do I convert ANSI files?

Step 1 – Open the Raw Data file in Notepad. Step 2 - Go to the File menu; choose "Save As". Step 3 - Change the Encoding option from UTF-8 to ANSI and save your file. Step 4 - Choose OK when a warning about converting to ANSI encoding appears.


2 Answers

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

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

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:

  • 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

UPDATE:

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

BinaryReader reader = new BinaryReader(File.Open("foo.txt", FileMode.Open),
                                       Encoding.GetEncoding(1252));
like image 94
Can Berk Güder Avatar answered Oct 03 '22 06:10

Can Berk Güder


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

// 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, text is actually unicode when in memory.

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

like image 24
Marc Gravell Avatar answered Oct 03 '22 06:10

Marc Gravell