Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an example which shows two methods, one based on the Qt library and one based on the ICU library. Both should be fairly platform-independent:</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;iomanip&gt; #include &lt;stdexcept&gt; #include &lt;boost/scoped_array.hpp&gt; #include &lt;QtCore/QString&gt; #include &lt;QtCore/QUrl&gt; #include &lt;QtCore/QVector&gt; #include &lt;unicode/utypes.h&gt; #include &lt;unicode/ustring.h&gt; #include &lt;unicode/unistr.h&gt; #include &lt;unicode/schriter.h&gt; void encodeQt() { const QString str = QString::fromWCharArray(L"ドイツ語で検索していてこちらのサイトにたどり着きました。"); const QUrl url = str; std::cout &lt;&lt; "URL encoded: " &lt;&lt; url.toEncoded().constData() &lt;&lt; std::endl; typedef QVector&lt;uint&gt; CodePointVector; const CodePointVector codePoints = str.toUcs4(); std::stringstream htmlEncoded; for (CodePointVector::const_iterator it = codePoints.constBegin(); it != codePoints.constEnd(); ++it) { htmlEncoded &lt;&lt; "&amp;#" &lt;&lt; *it &lt;&lt; ';'; } std::cout &lt;&lt; "HTML encoded: " &lt;&lt; htmlEncoded.str() &lt;&lt; std::endl; } void encodeICU() { const std::wstring cppString = L"ドイツ語で検索していてこちらのサイトにたどり着きました。"; int bufSize = cppString.length() * 2; boost::scoped_array&lt;UChar&gt; strBuffer(new UChar[bufSize]); int size = 0; UErrorCode error = U_ZERO_ERROR; u_strFromWCS(strBuffer.get(), bufSize, &amp;size, cppString.data(), cppString.length(), &amp;error); if (error) return; const UnicodeString str(strBuffer.get(), size); bufSize = str.length() * 4; boost::scoped_array&lt;char&gt; buffer(new char[bufSize]); u_strToUTF8(buffer.get(), bufSize, &amp;size, str.getBuffer(), str.length(), &amp;error); if (error) return; const std::string urlUtf8(buffer.get(), size); std::stringstream urlEncoded; urlEncoded &lt;&lt; std::hex &lt;&lt; std::setfill('0'); for (std::string::const_iterator it = urlUtf8.begin(); it != urlUtf8.end(); ++it) { urlEncoded &lt;&lt; '%' &lt;&lt; std::setw(2) &lt;&lt; static_cast&lt;unsigned int&gt;(static_cast&lt;unsigned char&gt;(*it)); } std::cout &lt;&lt; "URL encoded: " &lt;&lt; urlEncoded.str() &lt;&lt; std::endl; std::stringstream htmlEncoded; StringCharacterIterator it = str; while (it.hasNext()) { const UChar32 pt = it.next32PostInc(); htmlEncoded &lt;&lt; "&amp;#" &lt;&lt; pt &lt;&lt; ';'; } std::cout &lt;&lt; "HTML encoded: " &lt;&lt; htmlEncoded.str() &lt;&lt; std::endl; } int main() { encodeQt(); encodeICU(); } </code></pre>
    singulars
    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. 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