将非空终止的无符号字符数组复制到 std::string
如果数组是空终止,这将非常简单:
unsigned char u_array[4] = { 'a', 's', 'd', '\0' };
std::string str = reinterpret_cast<char*>(u_array);
std::cout << "-> " << str << std::endl;
但是,我想知道复制非空终止无符号字符数组的最合适方法是什么,例如以下内容:
unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
转换为 std::string
。
有没有办法在不迭代无符号字符数组的情况下做到这一点?
谢谢大家。
If the array was null-terminated this would be pretty straight forward:
unsigned char u_array[4] = { 'a', 's', 'd', '\0' };
std::string str = reinterpret_cast<char*>(u_array);
std::cout << "-> " << str << std::endl;
However, I wonder what is the most appropriate way to copy a non null-terminated unsigned char array, like the following:
unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
into a std::string
.
Is there any way to do it without iterating over the unsigned char array?
Thank you all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
std::string
有一个 构造函数采用一对迭代器,unsigned char
可以(以实现定义的方式)转换为char
,因此可以工作。不需要reinterpret_cast
。当然,“数组大小”模板函数比
sizeof
计算更稳健。std::string
has a constructor that takes a pair of iterators andunsigned char
can be converted (in an implementation defined manner) tochar
so this works. There is no need for areinterpret_cast
.Of course an "array size" template function is more robust than the
sizeof
calculation.好吧,显然 std::string 有一个可以在这种情况下使用的构造函数:
Well, apparently std::string has a constructor that could be used in this case:
当构造字符串而不指定其大小时,构造函数将迭代字符数组并查找空终止符,即
'\0'
字符。如果没有该字符,则必须显式指定长度,例如:When constructing a string without specifying its size, constructor will iterate over a a character array and look for null-terminator, which is
'\0'
character. If you don't have that character, you have to specify length explicitly, for example:std::string 有一个名为 allocate 的方法。您可以使用 char * 和大小。
http://www.cplusplus.com/reference/string/string/assign/
std::string has a method named assign. You can use a char * and a size.
http://www.cplusplus.com/reference/string/string/assign/
这应该可以做到:
This should do it:
您可以使用此
std::string
构造函数:因此在您的示例中:
You can use this
std::string
constructor:so in your example:
您可以创建一个指向第一个字符的字符指针,另一个指向最后一个字符的字符指针,并使用这两个指针作为迭代器进行构造。因此:
You can create a character pointer pointing to the first character, and another pointing to one-past-the-last, and construct using those two pointers as iterators. Thus:
当字符串本身包含空字符并且您随后尝试打印该字符串时,仍然存在问题:
但是......
当您只想放弃可爱并使用裸C时,就会出现这样的问题。
There is a still a problem when the string itself contains a null character and you try to subsequently print the string:
However....
Its times like these when you just want to ditch cuteness and use bare C.
尽管问题是如何“将非空终止的
unsigned char
数组 [...] 复制到std::string
”,但我注意到在给定的示例中该字符串仅用作 std::cout 的输入。在这种情况下,当然您可以完全避免使用字符串,而只需
这样做我认为可以解决OP试图解决的问题。
Although the question was how to "copy a non null-terminated
unsigned char
array [...] into astd::string
", I note that in the given example that string is only used as an input tostd::cout
.In that case, of course you can avoid the string altogether and just do
which I think may solve the problem the OP was trying to solve.
尝试:
Try:
std::string 有一个构造函数,采用 char 数组和长度。
std::string has a constructor taking an array of char and a length.
呃,为什么要选演员?
完毕。
Ew, why the cast?
Done.