Streamwriter Special Characters with Encoding.ASCII C#

Asked

Viewed 11,054 times

8

I am writing in a file via Streamwriter using the encoding Encoding.ASCII. A situation has appeared where I need to write the letter "Ç". Only if I try to do that, the file leaves the character "?".

In this case, I need to use this encoding (I cannot use UTF8). Is there any way I can treat this character and make it appear correctly in the file?

An excerpt from the code example:

StreamWriter streamArquivo = new StreamWriter(caminhoArquivo, false, Encoding.ASCII);
streamWriter.WriteLine("COBRANÇA");

I tried to see something related to the code of the letter "Ç" in byte, etc but the problem continues. Is there any way to do this?

  • 1

    Why can’t you use UTF-8? What else can’t you use?

  • @Bigown cannot use this encoding because it is a file used in communication with another system. If I use UTF-8 for example, and I open the file in the MSDOS editor, some characters appear as " " that disturb the communication.That’s why I opted for ASCII, but now I need this word to exit "Ç"

1 answer

10


The encoding the simplest that solves your problem is Latin1 (see table). In place of ASCII would use:

Encoding.GetEncoding("iso-8859-1")

or

Encoding.GetEncoding(28591)

I put in the Github for future reference.

Depending on your case you may have to use the 850 or 860.

If you cannot use it, the solution is to convert the text excluding the accents, has lost information, but is the only solution left.

  • Great!! I have a case using Encoding Utf8encoding also did not recognize some characters, being the "Ç" my main problem. In my case solved with the Encoding.GetEncoding(850).

  • worked here!! Thanks!

  • Thank you, you helped a lot here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.