Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>IMHO you should use a portable character codec library. Here's an example of minimal portable code using iconv, which should be more than enough. It's supposed to work on Windows (if it does, you can get rid of your windows-specific code altogether). I follow the GNU guidelines not to use the wcstombs &amp; co functions ( <a href="https://www.gnu.org/s/hello/manual/libc/iconv-Examples.html" rel="nofollow">https://www.gnu.org/s/hello/manual/libc/iconv-Examples.html</a> ) Depending on the use case, handle errors appropriately... and to enhance performance, you can create a class out of it.</p> <pre><code>#include &lt;iostream&gt; #include &lt;iconv.h&gt; #include &lt;cerrno&gt; #include &lt;cstring&gt; #include &lt;stdexcept&gt; std::string wstring_to_utf8_string(const std::wstring &amp;input) { size_t in_size = input.length() * sizeof(wchar_t); char * in_buf = (char*)input.data(); size_t buf_size = input.length() * 6; // pessimistic: max UTF-8 char size char * buf = new char[buf_size]; memset(buf, 0, buf_size); char * out_buf(buf); size_t out_size(buf_size); iconv_t conv_desc = iconv_open("UTF-8", "wchar_t"); if (conv_desc == iconv_t(-1)) throw std::runtime_error(std::string("Could not open iconv: ") + strerror(errno)); size_t iconv_value = iconv(conv_desc, &amp;in_buf, &amp;in_size, &amp;out_buf, &amp;out_size); if (iconv_value == -1) throw std::runtime_error(std::string("When converting: ") + strerror(errno)); int ret = iconv_close(conv_desc); if (ret != 0) throw std::runtime_error(std::string("Could not close iconv: ") + strerror(errno)); std::string s(buf); delete [] buf; return s; } int main() { std::wstring in(L"hello world"); std::wcout &lt;&lt; L"input: [" &lt;&lt; in &lt;&lt; L"]" &lt;&lt; std::endl; std::string out(wstring_to_utf8_string(in)); std::cerr &lt;&lt; "output: [" &lt;&lt; out &lt;&lt; "]" &lt;&lt; std::endl; return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload