I am aware that std::string
and std::wstring
come from the same base type std::basic_string<>
. But there isn't an "official" way to convert std::string
data to std::wstring
using the C++ STL? I mean Windows provide MultiByteToWideChar()
to convert but why cant the STL provide one?
I used std::codecvt
before to get it done but now it says that it is deprecated. Why does the STL remove this support the first place?
Thanks in advance.
The character encoding of std::string
is not defined by the C++ standard, a std::string
can hold any encoding that can be represented using 1-byte char
elements, which includes UTF-7/8, ISO-8859-x, Windows-125x, etc.
Also, the size of wchar_t
is implementation-defined , not defined by the standard, so even the encoding of std::wstring
can vary, too. On Windows, wchar_t
is 2 bytes, so std::wstring
uses UCS-2/UTF-16 encoding. Whereas on other platforms, wchar_t
is 4 bytes, so std::wstring
uses UCS-4/UTF-32.
So, there is no single conversion that satisfies all possible combinations of std::string <-> std::wstring
conversions across all platforms and use-cases. So, you need to know the encoding of the source string, and the intended encoding of the target string, in order to perform a conversion.
And yes, the C++ standard did provide std::codecvt
and std::wstring_convert
/ std::wbuffer_convert
for this task, but they have been deprecated, as you have noted. There is no standard replacement provided (yet?).
So, you are best off using 3rd party Unicode API/libraries to handle character conversions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.