[英]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? 因此,给定一个基本的
std::string
其字符范围为-128至127,我如何轻松地将其转换为一个std::ustring
其字符范围为0至255?
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
. basic_string<unsigned char>
将需要特殊化char_traits
。 And no new names can be added to namespace std
. 而且不能将任何新名称添加到命名空间
std
。
You may use the iterator constructor: 您可以使用迭代器构造函数:
ustring to_ustring(const std::string& s)
{
return {s.begin(), s.end()};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.