5

Есть текст

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

Артемий декодер говорит что это cp1251 Я пробую его перевести в utf-8 однако на выходе еще хуже крякозябры.

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;

        }

текст считывается из ini-файла. Через notepad++ просмотрел - все норм с кодировкой. Отсюда следует что проблема в следующем классе для чтения ini-файлов.

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;
        }

    }
4
  • Вы делаете неправильно. Откуда вы взяли этот текст? Это важно.
    – VladD
    15 сен 2016 в 20:10
  • дополнил.........
    – asda
    15 сен 2016 в 20:18
  • Ну так что.......
    – asda
    15 сен 2016 в 21:09
  • Окей, а какую кодировку показывает Notepad++?
    – VladD
    15 сен 2016 в 21:25

2 ответа 2

13
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);

Console.WriteLine(win1251.GetString(win1251Bytes));

Output:

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

1
  • 1
    Логически перепутаны местами переменные utf8Bytes и win1251Bytes
    – menkow
    4 сен 2017 в 13:03
0

Если выводится ошибка, то стоит попробовать

Encoding win1251 = CodePagesEncodingProvider.Instance.GetEncoding(1251);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.