c++11stlcharstdstringchar32-t

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


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.


Solution

  • 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;}