How to easily convert an unsigned string to a string?

I define an unsigned string as a string containing unsigned chars:

namespace std
{
   typedef basic_string<unsigned char> ustring;
}

Thus, given a basic std::string whose chars range from -128 to 127, how can I easily convert it to a std::ustring whose chars range from 0 to 255?


你可以使用迭代器构造函数:

ustring to_ustring(const std::string& s)
{
    return {s.begin(), s.end()};
}

You do:

using ustring = std::vector<unsigned char>;
std::string a("abc");
ustring b(a.begin(), a.end()); 

Note that the zero-terminator is not copied.


basic_string<unsigned char> would require specializing char_traits . And no new names can be added to namespace std .

链接地址: http://www.djcxy.com/p/96708.html

上一篇: 在c中的signed和unsigned char之间的区别

下一篇: 如何轻松将无符号字符串转换为字符串?