Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File read foreign characters

I am trying to read foreign characters from an .ini file. This is the method I am using

[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string Section, 
                                                 int Key,
                                                 string Value,
                                                 [MarshalAs(UnmanagedType.LPArray)] byte[] Result,
                                                 int Size, 
                                                 string FileName);

public static string[] GetEntryNames(string section, string iniPath)
    {
        for (int maxsize = 500; true; maxsize *= 2)
        {
            byte[] bytes = new byte[maxsize];
            int size = Imports.GetPrivateProfileString(section, 0, "", bytes, maxsize, iniPath);
            if (size < maxsize - 2)
            {
                string entries = Encoding.ASCII.GetString(bytes, 0,
                                          size - (size > 0 ? 1 : 0));
                Console.WriteLine("Entries: " + entries.Split(new char[] { '\0' })[3]);
                return entries.Split(new char[] { '\0' });
            }
        }
    }

I am using Encoding.ASCII but apparently GetPrivateProfileString isn't. The bytes coming out of it need encoding probably. How can I do that?

Edit: Example

This will print : Tavar? instead of Tavaré

like image 456
John P. Avatar asked May 09 '16 21:05

John P.


1 Answers

To read text that contains french accents, you usually want to use one of the following encodings :

  • Encoding.UTF8
  • Encoding.GetEncoding("iso-8859-1")
  • Encoding.GetEncoding("windows-1252")

Edit: These worked for OP :

  • Encoding.UTF7
  • Encoding.GetEncoding("iso-8859-1")
like image 88
Xiaoy312 Avatar answered Sep 21 '22 08:09

Xiaoy312