title: Utf-8 to URI percent encoding
body: <p>I'm trying to convert Unicode code points to percent encoded UTF-8 code units.</p>
  
  <p>The Unicode -> UTF-8 conversion seems to be working correctly as shown by some testing with Hindi and Chinese characters which show up correctly in Notepad++ with UTF-8 encoding, and can be translated back properly.</p>
  
  <p>I thought the percent encoding would be as simple as adding '%' in front of each UTF-8 code unit, but that doesn't quite work. Rather than the expected <strong>%E5%84%A3</strong>, I'm seeing <strong>%xE5%x84%xA3</strong> (for the unicode U+5123).</p>
  
  <p><img src="https://i.stack.imgur.com/QkHdD.png" alt="enter image description here"></p>
  
  <p>What am I doing wrong?</p>
  
  <p>Added code (note that utf8.h belongs to the UTF8-CPP library). </p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include "utf8.h"
  
  std::string unicode_to_utf8_units(int32_t unicode)
  {
      unsigned char u[5] = {0,0,0,0,0};
      unsigned char *iter = u, *limit = utf8::append(unicode, u);
      std::string s;
      for (; iter != limit; ++iter) {
          s.push_back(*iter);
      }
      return s;
  }
  
  int main()
  {
      std::ofstream ofs("test.txt", std::ios_base::out);
      if (!ofs.good()) {
          std::cout &lt;&lt; "ofstream encountered a problem." &lt;&lt; std::endl;
          return 1;
      }
  
      utf8::uint32_t unicode = 0x5123;
      auto s = unicode_to_utf8_units(unicode);
      for (auto &amp;c : s) {
          ofs &lt;&lt; "%" &lt;&lt; c;
      }
  
      ofs.close();
  
      return 0;
  }
  </code></pre>