Last post Apr 10, 2011 08:24 AM by lelo66sz
Feb 01, 2011 12:35 PM|Otavio Camargo|LINK
I'm trying to convert text from UTF8 to ASCII.
I need to generate a text file with ASCII encoding. But when I create the file, special caracters are wrote with unknown format.
Could any one say what I'm doing wrong in the code bellow?
FileStream textStream = File.Open(caminhoArquivoTexto, FileMode.Create, FileAccess.Write);
StreamWriter textWriter = new StreamWriter(textStream, Encoding.ASCII);
StringBuilder registro = new StringBuilder();
registro.Append("Otávio Camaçarão"); //Name with special caracters
ASCIIEncoding ascii = new ASCIIEncoding();
byte byteArray = Encoding.UTF8.GetBytes(registro.ToString());
byte asciiArray = Encoding.Convert(Encoding.UTF8, Encoding.ASCII, byteArray);
string finalString = ascii.GetString(asciiArray);
Feb 01, 2011 12:52 PM|raduenuca|LINK
Simply stated ASCII is a subseft of UTF-8. So there will be UTF-8 codepoint that have no correspondend in ASCII hence the funcky characters shwon.
See this for more info:
Feb 01, 2011 12:54 PM|HeartattacK|LINK
ASCIIEncoding encodes Unicode characters as single 7-bit ASCII characters. This encoding only supports character values between U+0000 and U+007F. This limitation
makes ASCII largely inadequate for internationalized applications. Consider using UTF8Encoding or UnicodeEncoding instead.
Feb 01, 2011 02:34 PM|Otavio Camargo|LINK
I need the text file to be saved in ANSI mode, which I think is the ASCII Encoder equivalent in C#.
If the text file is in a format different from ANSI, another application which will use the file doesn't recognize it as a valid text file.
Isn't there any way to save the file in ANSI mode and recognise special caracters?
Feb 01, 2011 04:38 PM|Otavio Camargo|LINK
I found out the problem.
I must use the encoding type System.Text.Encoding.Default.
This the same as the ANSI format.
Apr 10, 2011 08:24 AM|lelo66sz|LINK
Nothing helped to me, except:
File.AppendAllText(file, fileContent, Encoding.GetEncoding(28591);