将 Unicode 转换为多字节

2021-12-22 00:00:00 visual-c++ c++

我有一个小问题,我想将 unicode 转换为多字节有什么办法

I have smalll problem i want to convert unicode into Multi byte is there any way

推荐答案

std::string NarrowString(const std::wstring& str, const char* localeName = "C")
{
  std::string result;
  result.resize(str.size());

  std::locale loc(localeName);

  std::use_facet<std::ctype<wchar_t> >(loc).narrow(
    str.c_str(), str.c_str() + str.size(), '?',  &*result.begin());

  return result;
}

它应该使用当前的语言环境来转换 unicode 字符串.对于不属于代码页的字符,'?'正在使用字符.使用 Visual C++ 2005/2008 测试.

It should use the current locale to convert the unicode string. For the caracters that do not belong in the codepage the '?' caracter is being used. Tested with Visual C++ 2005/2008.

相关文章