title: How can I to get the utf-8 int value of a unicode char from a (w)string?
body: <h2>Situation</h2>
  
  <p>I need a function that expects a string and encodes all non-ascii chars to utf-8 as hexadecimal number and substitutes it with that.</p>
  
  <p>For example, ӷ in a word like "djvӷdio" should be substituted with "d3b7" while the rest remains untouched.</p>
  
  <pre class="lang-sh prettyprint-override"><code>Explanation:
  ӷ equals int 54199 and in hexadecimal d3b7
  djvӷdio --&gt; djvd3b7dio
  </code></pre>
  
  <p><strong>I already have a function that returns the hex value of an int.</strong></p>
  
  <h2>My Machine</h2>
  
  <ul>
  <li>kubuntu, 19.10</li>
  <li>Compiler: g++ (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008</li>
  </ul>
  
  <h2>My Ideas</h2>
  
  <h1>1. Idea</h1>
  
  <pre class="lang-cpp prettyprint-override"><code>std::string encode_utf8(const std::string &amp;str);
  </code></pre>
  
  <p>With the use of the function above I iterate through the whole string which contains unicode and if the current char is non-ascii I replace it with its hex value.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>Iterating through a string with unicode is not clever as a unicode char is made out of up to 4 bytes unlike a normal char. Therefore, a <strong>unicode char can be treated as multiple chars</strong> which outputs garbage. In easy words, the string cannot be indexed.</p>
  
  <h1>2. Idea</h1>
  
  <pre class="lang-cpp prettyprint-override"><code>std::string encode_utf8(const std::wstring &amp;wstr);
  </code></pre>
  
  <p>Again, I iterate through the whole string with unicode chars and if the current char is non-ascii I replace it with its hex value.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>Indexing works now but it returns a wchar_t with the corresponding utf-32 number but I definitely need the utf-8 number.</p>
  
  <hr>
  
  <p><strong>How can I get a char out of a string from which I can get the utf-8 decimal number?</strong></p>