Is there any built-in function that convert wstring or wchar_t* to UTF-8 in Linux?

孤人 提交于 2019-12-01 16:03:52

问题


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.

回答1:


The C++ language standard has no notion of explicit encodings. It only contains an opaque notion of a "system encoding", for which wchar_t is 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() (from WCHAR_T to UTF-8), which is part of Posix and available on many platforms, although on Windows the WideCharToMultibyte functions 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 opaque wstring other than through the way I described.

See this question for a bit more background.




回答2:


If/when your compiler supports enough of C++11, you could use wstring_convert

#include <iostream>
#include <codecvt>
#include <locale>
int main()
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> utf8_conv;
    std::wstring str = L"file_name.txt";
    std::cout << utf8_conv.to_bytes(str) << '\n';
}

tested with clang++ 2.9/libc++ on Linux and Visual Studio 2010 on Windows.




回答3:


It's quite plausible that wcstombs will do what you need if what you actually want to do is convert from wide characters to the current locale.

If not then you probably need to look to ICU, boost or similar.




回答4:


Certainly there is no function built in on Linux, because the name Linux references the kernel only, which doesn't have anything to with it. I seriously doubt that the libc that comes with gcc has such a function, and

$ man -k utf

supports this theory. But there are plenty of good UTF-8 libraries around. I personally recommend the iconv library for such conversions.



来源:https://stackoverflow.com/questions/7469296/is-there-any-built-in-function-that-convert-wstring-or-wchar-t-to-utf-8-in-linu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!