c# – How to convert text from 1251 to utf-8


There is a text

Заказ звонка технической поддержки

Artemy the decoder says that this is cp1251. I'm trying to convert it to utf-8, but the output is even worse than kryakozyabry.

private string Win1251ToUTF8(string source)

            Encoding utf8 = Encoding.GetEncoding("utf-8");
            Encoding win1251 = Encoding.GetEncoding("windows-1251");

            byte[] utf8Bytes = win1251.GetBytes(source);
            byte[] win1251Bytes = Encoding.Convert(win1251, utf8, utf8Bytes);
            source = win1251.GetString(win1251Bytes);
            return source;


the text is read from the ini file. Through notepad ++ looked – all the rules with encoding. It follows that the problem is in the next class for reading ini-files.

class IniFile   // revision 11
        string Path;
        string EXE = Assembly.GetExecutingAssembly().GetName().Name;

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);

        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);

        public IniFile(string IniPath = null)
            Path = new FileInfo(IniPath ?? EXE + ".ini").FullName.ToString();

        public string Read(string Key, string Section = null)
            var RetVal = new StringBuilder(255);
            GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);

            return RetVal.ToString();

        public void Write(string Key, string Value, string Section = null)
            WritePrivateProfileString(Section ?? EXE, Key, Value, Path);

        public void DeleteKey(string Key, string Section = null)
            Write(Key, null, Section ?? EXE);

        public void DeleteSection(string Section = null)
            Write(null, null, Section ?? EXE);

        public bool KeyExists(string Key, string Section = null)
            return Read(Key, Section).Length > 0;



string text = "Заказ звонка технической поддержки";

Encoding utf8 = Encoding.GetEncoding("UTF-8");
Encoding win1251 = Encoding.GetEncoding("Windows-1251");

byte[] utf8Bytes = win1251.GetBytes(text);
byte[] win1251Bytes = Encoding.Convert(utf8, win1251, utf8Bytes);



Request a technical support call

Scroll to Top