How to convert UTF-8 std::string to UTF-16 std::wstring?

This is how you do it with C++11:

std::string str = "your string in utf8";
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>> converter;
std::wstring wstr = converter.from_bytes(str);

And these are the headers you need:

#include <iostream>
#include <string>
#include <locale>
#include <codecvt>

A more complete example available here:
http://en.cppreference.com/w/cpp/locale/wstring_convert/from_bytes

Leave a Comment