title: C++ & Boost: encode/decode UTF-8
body: <p>I'm trying to do a very simple task: take a unicode-aware <code>wstring</code> and convert it to a <code>string</code>, encoded as UTF8 bytes, and then the opposite way around: take a <code>string</code> containing UTF8 bytes and convert it to unicode-aware <code>wstring</code>.</p>
  
  <p>The problem is, I need it cross-platform and I need it work with Boost... and I just can't seem to figure a way to make it work. I've been toying with</p>
  
  <ul>
  <li><a href="http://www.edobashira.com/2010/03/using-boost-code-facet-for-reading-utf8.html" rel="noreferrer">http://www.edobashira.com/2010/03/using-boost-code-facet-for-reading-utf8.html</a> and</li>
  <li><a href="http://www.boost.org/doc/libs/1_46_0/libs/serialization/doc/codecvt.html" rel="noreferrer">http://www.boost.org/doc/libs/1_46_0/libs/serialization/doc/codecvt.html</a></li>
  </ul>
  
  <p>Trying to convert the code to use <code>stringstream</code>/<code>wstringstream</code> instead of files of whatever, but nothing seems to work.</p>
  
  <p>For instance, in Python it would look like so:</p>
  
  <pre><code>&gt;&gt;&gt; u"שלום"
  u'\u05e9\u05dc\u05d5\u05dd'
  &gt;&gt;&gt; u"שלום".encode("utf8")
  '\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d'
  &gt;&gt;&gt; '\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d'.decode("utf8")
  u'\u05e9\u05dc\u05d5\u05dd'
  </code></pre>
  
  <p>What I'm ultimately after is this:</p>
  
  <pre><code>wchar_t uchars[] = {0x5e9, 0x5dc, 0x5d5, 0x5dd, 0};
  wstring ws(uchars);
  string s = encode_utf8(ws); 
  // s now holds "\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d"
  wstring ws2 = decode_utf8(s);
  // ws2 now holds {0x5e9, 0x5dc, 0x5d5, 0x5dd}
  </code></pre>
  
  <p>I really don't want to add another dependency on the ICU or something in that spirit... but to my understanding, it should be possible with Boost.</p>
  
  <p>Some sample code would greatly be appreciated! Thanks</p>