-2

In a nutshell, creating a ASCII text file that records polling data from a printer under test. The expected output should be US English, but the text file is in Vietnamese. Example: the_Maker = "Epson", SubModel = "T88V", serial_num = PD9F393594, error_list = total # of polling errors (a number). - Example if testing a Epson T88V Thermal Printer.

using (System.IO.StreamWriter file = new System.IO.StreamWriter(output_file, true))                
{
    ASCIIEncoding ascii = new ASCIIEncoding();
    file.WriteLine(the_Maker + " ● " + SubModel + " ● " + serial_num + " ● " + error_list + " END Polls " + DateTime.Now.ToString());
}

Output from file: 偅体辗䵔吭㠸辗䑐䘹㤳㔳㐹韢䖏䑎倠汯獬㔠㈯⼷〲㤱㔠㌺㨶㈵倠്䔊卐乏辗吠ⵍ㡔嘸辗倠㥄㍆㌹㤵‴韢₏䔠䑎倠汯獬㔠㈯⼷〲㤱㔠㌺㨸㘲倠്

This should be in English

Daniel Steele
  • 33
  • 1
  • 1
  • 7
  • What do the variables in your code contain? we need a [mcve] to help you. You aren't using `ascii` anywhere. –  May 27 '19 at 21:52
  • 1
    Please add that information *to the question*, not in a comment. People shouldn't need to hunt around for a complete picture of what's happening. Edit your question and expand it into a [mcve]. –  May 27 '19 at 21:55
  • 1
    How did you conclude that was Vietnamese? It looks distinctively Chinese, which is what you get when you interpret a file in ASCII as a file in UTF-16. Your file *is* in ASCII, but the tool you are using it to view it thinks it is in UTF-16. – GSerg May 27 '19 at 22:17
  • 3
    It is encoded in utf8, the default encoding for StreamWriter, spells "EPSON●TM-T88V●PD9F393594●END Polls 5/27/2019 5:36:52 PM ...". Whatever you used to look at the file did not realize it is utf8 and assumed utf16. So you might get ahead when you pass Encoding.Unicode to the StreamWriter constructor. But you'd better focus a bit on what the printer needs. – Hans Passant May 27 '19 at 22:17
  • 2
    [You aren't opening the file in Notepad are you?](https://devblogs.microsoft.com/oldnewthing/20040324-00/?p=40093) – Dour High Arch May 27 '19 at 22:30
  • Tried opening in Notepad & WordPad. – Daniel Steele May 27 '19 at 23:17

1 Answers1

0

If you intend to use an encoding for the StreamWriter, you have to construct the StreamWriter with the target encoding. You are currently creating an ASCIIEncoding object and not using it.

ASCIIEncoding ascii = new ASCIIEncoding();
using (System.IO.StreamWriter file = new System.IO.StreamWriter(output_file, true, ascii))                
{
    file.WriteLine(the_Maker + " ● " + SubModel + " ● " + serial_num + " ● " + error_list + " END Polls " + DateTime.Now.ToString());
}
vinicius.ras
  • 1,516
  • 3
  • 14
  • 28
  • Pasted your code (Remarked out my code), output is still: 偅体⁎‿䵔吭㠸⁖‿䑐䘹㤳㔳㐹㼠†久⁄潐汬⁳⼵㜲㈯㄰‹㨶㜰㈺‵䵐਍ Should be in US English, I'm not under standing the disconnect. I'm in USA, Installed Windows for English, Installed Visual Studio in English, but file output is not English. Where did I go wrong? – Daniel Steele May 27 '19 at 22:08
  • I have confirmed my Region & Language is: United States, Keyboard is English United States, no other languages installed. – Daniel Steele May 27 '19 at 22:16
  • I have just tested this code by forcing the usage of "en-US" culture and it works correctly for writing files. I re-read your question and I am guessing that the problem is the output device interpreting characters in another way, other than ASCII. – vinicius.ras May 27 '19 at 22:21
  • How did you "force" it? – Daniel Steele May 27 '19 at 22:29
  • You can manually set the current executing thread's culture to a specific culture like: `Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");` – vinicius.ras May 27 '19 at 22:30
  • 1
    Adding: UTF32Encoding ascii = new UTF32Encoding(); resolved the issue, Thank you all for your help! – Daniel Steele May 27 '19 at 23:35
  • 1
    @DanielSteele UTF32 did not solve your problem because you did not have a problem to begin with. You had a file in ASCII which your tool interpreted as UTF-16 which is why you saw Chinese characters. After encoding same data in UTF-32, the tool is not not confused anymore, but your file now takes 4 times more space. – GSerg May 28 '19 at 07:46