7

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

 2 years ago
source link: https://devblogs.microsoft.com/oldnewthing/20210922-00/?p=105717
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

Raymond

September 22nd, 2021

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK