Converting between UTF-8 strings and UTF-16 strings in C++/WinRT

Raymond Chen

C++/WinRT provides a pair of functions for converting between UTF-8 strings (with code units represented as char) and UTF-16 strings (code units of wchar_t).

The to_string function takes a std::wstring_view of UTF-16 code units and converts them to a UTF-8 string, represented as a std::string.

Conversely, the to_hstring function takes a std::string_view of UTF-8 code units and converts them to a UTF-16 string, represented as a winrt::hstring.

The argument to to_string and to_hstring can be anything convertible to the corresponding string view type. Types that fall into this category include

Type Converts to
std::string std::string_view
std::wstring std::wstring_view
winrt::hstring std::wstring_view

We’ll put these conversions to good use next time.