C#: Cycle through encodings

后端 未结 6 1487
小蘑菇
小蘑菇 2020-12-16 05:42

I am reading files in various formats and languages and I am currently using a small encoding library to take attempt to detect the proper encoding (http://www.codeproject.c

6条回答
  •  [愿得一人]
    2020-12-16 06:36

    Read the file as bytes and use then the Encoding.GetString Method.

            byte[] data = System.IO.File.ReadAllBytes(path);
    
            Console.WriteLine(Encoding.UTF8.GetString(data));
            Console.WriteLine(Encoding.UTF7.GetString(data));
            Console.WriteLine(Encoding.ASCII.GetString(data));
    

    So you have to load the file only one time. You can use every encoding based on the original bytes of the file. The user can select the correct one und you can use the result of Encoding.GetEncoding(...).GetString(data) for further processing.

提交回复
热议问题