{"id":105717,"date":"2021-09-22T07:00:00","date_gmt":"2021-09-22T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=105717"},"modified":"2021-09-22T06:37:03","modified_gmt":"2021-09-22T13:37:03","slug":"20210922-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210922-00\/?p=105717","title":{"rendered":"Converting between UTF-8 strings and UTF-16 strings in C++\/WinRT"},"content":{"rendered":"<p>C++\/WinRT provides a pair of functions for converting between UTF-8 strings (with code units represented as <code>char<\/code>) and UTF-16 strings (code units of <code>wchar_t<\/code>).<\/p>\n<p>The <code>to_string<\/code> function takes a <code>std::wstring_view<\/code> of UTF-16 code units and converts them to a UTF-8 string, represented as a <code>std::string<\/code>.<\/p>\n<p>Conversely, the <code>to_hstring<\/code> function takes a <code>std::string_view<\/code> of UTF-8 code units and converts them to a UTF-16 string, represented as a <code>winrt::hstring<\/code>.<\/p>\n<p>The argument to <code>to_string<\/code> and <code>to_hstring<\/code> can be anything convertible to the corresponding string view type. Types that fall into this category include<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>Type<\/th>\n<th>Converts to<\/th>\n<\/tr>\n<tr>\n<td><code>std::string<\/code><\/td>\n<td><code>std::string_view<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>std::wstring<\/code><\/td>\n<td><code>std::wstring_view<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>winrt::hstring<\/code><\/td>\n<td><code>std::wstring_view<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>We&#8217;ll put these conversions to good use next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A pair of conversion functions.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-105717","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A pair of conversion functions.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=105717"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105717\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=105717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=105717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=105717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}