1

I have std::string utf16 containing text in utf16. This string I get from third-party library. For example std::string utf16("\x04\x1f\x04\x40\x04\x38\x04\x32\x04\x35\x04\x42"). This text is equal Russian word Привет. How can I convert utf16 to std::string utf8 containing utf8? In this instance utf8 looks like std::string utf8("\xd0\x9f\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82").

German
  • 11
  • 1
  • Look into utilizing 3rd party libraries. Qt can help. Also a post that might help as well: https://stackoverflow.com/questions/38688417/utf-conversion-functions-in-c11 – Omid CompSCI Nov 11 '21 at 14:19
  • 3
    @OmidCompSCI Integrating QT within a project just to provide utf16->utf8 conversion is, to put it lightly, extremely overkill. –  Nov 11 '21 at 14:22
  • 1
    Also, a word of warning about that linked question: `wstring_convert` has since been removed from the standard library, so the accepted answer is not really a good recommendation anymore. –  Nov 11 '21 at 14:25
  • This is one of those few topics where the "good" answer is a library recommendation IMO. My poisons of choice these days are [nemtrif/utfcpp](https://github.com/nemtrif/utfcpp) for lightweight stuff, and [ICU](https://icu.unicode.org/) where true robustness is required. –  Nov 11 '21 at 14:31
  • I use c++17. I think I will use nemtrif/utfcpp. But is there such a possibility in c++17? – German Nov 11 '21 at 14:51
  • @German Not out of the box. You have to either provide your own conversion function or use a library. –  Nov 11 '21 at 14:59
  • i use `#include #include #include const char *str = "\x04\x25\x0"; std::u16string to_utf16(const std::string &s) { std::wstring_convert, char16_t> convert; return convert.from_bytes(s); } std::string to_utf8(const std::u16string &s) { std::wstring_convert, char16_t> conv; return conv.to_bytes(s); } int main(){ std::string s(str); std::string new_str = to_utf8(to_utf16(s)); std::cout << new_str; return 0; }` But I get source string. What am I doing wrong? – German Nov 11 '21 at 15:08
  • @German That's a question in of itself. You should post it as another question, containing that code, what it outputs and what you expect it to output. –  Nov 11 '21 at 15:19
  • But FYI, codecvt is already officially deprecated as of C++17. –  Nov 11 '21 at 15:21

0 Answers0