winapi — конвертирование кодовых страниц в переполнение стека

Что эквивалентно коду преобразования строки C # (между кодовыми страницами):

public static string Convert(string s)
{
Encoding encoder = Encoding.GetEncoding(858);
return Encoding.Default.GetString(encoder.GetBytes(s));
}

в VC ++ (не CLR), например используя функции WinAPI WideCharToMultiByte / MultiByteToWideChar?

-2

Решение

Да, MultiByteToWideChar() а также WideCharToMultiByte() являются эквивалентными функциями Win32, например:

std::wstring Convert(const std::wstring &s)
{
if (s.empty())
return std::wstring();

int len = WideCharToMultiByte(858, 0, s.c_str(), s.length(), NULL, 0, NULL, NULL);
if (len == 0)
throw std::runtime_error("WideCharToMultiByte() failed");

std::vector<char> bytes(len);

len = WideCharToMultiByte(858, 0, s.c_str(), s.length(), &bytes[0], len, NULL, NULL);
if (len == 0)
throw std::runtime_error("WideCharToMultiByte() failed");

len = MultiByteToWideChar(CP_ACP, 0, &bytes[0], bytes.size(), NULL, 0);
if (len == 0)
throw std::runtime_error("MultiByteToWideChar() failed");

std::wstring result;
result.resize(len);

len = MultiByteToWideChar(CP_ACP, 0, &bytes[0], bytes.size(), &result[0], len);
if (len == 0)
throw std::runtime_error("MultiByteToWideChar() failed");

return result;
}
2

Другие решения


По вопросам рекламы ammmcru@yandex.ru
Adblock
detector