I want to convert wstring to UTF-8 Encoding, but I want to use built-in functions of Linux.
Is there any built-in function that convert wstring or wchar_t* to UTF-8 in Linux with simple invokation?
Example:
wstring str = L"file_name.txt";
wstring mode = "a";
fopen([FUNCTION](str), [FUNCTION](mode)); // Simple invoke.
cout << [FUNCTION](str); // Simple invoke.
The C++ language standard has no notion of explicit encodings. It only contains an opaque notion of a “system encoding”, for which
wchar_tis a “sufficiently large” type.To convert from the opaque system encoding to an explicit external encoding, you must use an external library. The library of choice would be
iconv()(fromWCHAR_TtoUTF-8), which is part of Posix and available on many platforms, although on Windows theWideCharToMultibytefunctions is guaranteed to produce UTF8.C++11 adds new UTF8 literals in the form of
std::string s = u8"Hello World: \U0010FFFF";. Those are already in UTF8, but they cannot interface with the opaquewstringother than through the way I described.See this question for a bit more background.