C# Help reading foreign characters using StreamReader

后端 未结 10 1261
别那么骄傲
别那么骄傲 2020-11-29 02:46

I\'m using the code below to read a text file that contains foreign characters, the file is encoded ANSI and looks fine in notepad. The code below doesn\'t work, when the fi

相关标签:
10条回答
  • 2020-11-29 03:05

    You may also try the Default encoding, which uses the current system's ANSI codepage.

    StreamReader reader = new StreamReader(inputFilePath, Encoding.Default, true)
    

    When you try using the Notepad "Save As" menu with the original file, look at the encoding combo box. It will tell you which encoding notepad guessed is used by the file.

    Also, if it is an ANSI file, the detectEncodingFromByteOrderMarks parameter will probably not help much.

    0 讨论(0)
  • 2020-11-29 03:06

    I had the same problem and my solution was simple: instead of

    Encoding.ASCII
    

    use

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

    The answer was found here.

    Edit: more solutions. This maybe more accurate one:

    Encoding.GetEncoding(1252);
    

    Also, in some cases this will work for you too if your OS default encoding matches file encoding:

    Encoding.Default;
    
    0 讨论(0)
  • 2020-11-29 03:07

    Using Encoding.Unicode won't accurately decode an ANSI file in the same way that a JPEG decoder won't understand a GIF file.

    I'm surprised that Encoding.Default didn't work for the ANSI file if it really was ANSI - if you ever find out exactly which code page Notepad was using, you could use Encoding.GetEncoding(int).

    In general, where possible I'd recommend using UTF-8.

    0 讨论(0)
  • 2020-11-29 03:08

    For swedish Å Ä Ö the only solution form the ones above working was:

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

    Hopefully this will save someone time.

    0 讨论(0)
  • 2020-11-29 03:10

    for Arabic, I used Encoding.GetEncoding(1256). it is working good.

    0 讨论(0)
  • 2020-11-29 03:18

    File.OpenText() always uses an UTF-8 StreamReader implicitly. Create your own StreamReader instance instead and specify the desired encoding. like

    using (StreamReader reader =  new StreamReader(@"C:\test.txt", Encoding.Default)
     {
     // ...
     }
    
    0 讨论(0)
提交回复
热议问题