How to convert std::string to std::u32string?

3.4k views Asked by At

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.

2

There are 2 answers

5
Mike Seymour On BEST ANSWER

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;}
1
Photon On
s32.resize(s.length());
std::copy(s.begin(),s.end(),s32.begin());