UTF8 to/from wide char conversion in STL

后端 未结 10 988
面向向阳花
面向向阳花 2020-11-22 06:48

Is it possible to convert UTF8 string in a std::string to std::wstring and vice versa in a platform independent manner? In a Windows application I would use MultiByteToWideC

10条回答
  •  忘掉有多难
    2020-11-22 07:12

    You can extract utf8_codecvt_facet from Boost serialization library.

    Their usage example:

      typedef wchar_t ucs4_t;
    
      std::locale old_locale;
      std::locale utf8_locale(old_locale,new utf8_codecvt_facet);
    
      // Set a New global locale
      std::locale::global(utf8_locale);
    
      // Send the UCS-4 data out, converting to UTF-8
      {
        std::wofstream ofs("data.ucd");
        ofs.imbue(utf8_locale);
        std::copy(ucs4_data.begin(),ucs4_data.end(),
              std::ostream_iterator(ofs));
      }
    
      // Read the UTF-8 data back in, converting to UCS-4 on the way in
      std::vector from_file;
      {
        std::wifstream ifs("data.ucd");
        ifs.imbue(utf8_locale);
        ucs4_t item = 0;
        while (ifs >> item) from_file.push_back(item);
      }
    

    Look for utf8_codecvt_facet.hpp and utf8_codecvt_facet.cpp files in boost sources.

提交回复
热议问题