Reputation: 800
Is there an easy STL way to convert a std::string to a std::u32string, i.e. a basic_string of char to char32_t?
This is not a Unicode question.
Upvotes: 0
Views: 3845
Reputation: 254691
To initialise a new string:
std::u32string s32(s.begin(), s.end());
To assign to an existing string:
s32.assign(s.begin(), s.end());
If the string might contain characters outside the supported range of char
, then this might cause sign-extension issues, converting negative values into large positive values. Dealing with that possibility is messier; you'll have to convert to unsigned char
before widening the value.
s32.resize(s.size());
std::transform(s.begin(), s.end(), s32.begin(),
[](char c) -> unsigned char {return c;});
or a plain loop
s32.clear(); // if not already empty
for (unsigned char c : s) {s32 += c;}
Upvotes: 1