C++ 将非空终止的无符号字符数组复制到 std::string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4691608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copying non null-terminated unsigned char array to std::string
提问by karlphillip
If the array was null-terminatedthis would be pretty straight forward:
如果数组以空值结尾,这将非常简单:
unsigned char u_array[4] = { 'a', 's', 'd', 'unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
' };
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-terminatedunsigned char array, like the following:
但是,我想知道复制非以 null 结尾的unsigned char 数组的最合适方法是什么,如下所示:
unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
#include <string>
#include <iostream>
#include <ostream>
int main()
{
std::string str( u_array, u_array + sizeof u_array / sizeof u_array[0] );
std::cout << str << std::endl;
return 0;
}
into a std::string
.
成std::string
.
Is there any way to do it without iterating over the unsigned char array?
有没有办法在不迭代无符号字符数组的情况下做到这一点?
Thank you all.
谢谢你们。
回答by CB Bailey
std::string
has a constructorthat takes a pair of iterators and unsigned char
can be converted (in an implementation defined manner) to char
so this works. There is no need for a reinterpret_cast
.
std::string
有一个构造函数,它接受一对迭代器,并且unsigned char
可以转换(以实现定义的方式),char
所以这是有效的。不需要一个reinterpret_cast
.
std::string str(reinterpret_cast<char*>(u_array), 4);
Of course an "array size" template function is more robust than the sizeof
calculation.
当然,“数组大小”模板函数比sizeof
计算更健壮。
回答by karlphillip
Well, apparently std::stringhas a constructorthat could be used in this case:
好吧,显然std::string有一个可以在这种情况下使用的构造函数:
// --*-- C++ --*--
#include <string>
#include <iostream>
int
main ()
{
unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
std::string str (reinterpret_cast<const char *> (u_array),
sizeof (u_array) / sizeof (u_array[0]));
std::cout << "-> " << str << std::endl;
}
回答by karlphillip
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:
当构造一个字符串而不指定其大小时,构造函数将遍历一个字符数组并查找空终止符,即'\0'
字符。如果您没有该字符,则必须明确指定长度,例如:
string ( const char * s, size_t n );
回答by foobar1234
std::string has a method named assign. You can use a char * and a size.
std::string 有一个名为assign 的方法。您可以使用字符 * 和大小。
回答by Benoit Thiery
You can use this std::string
constructor:
您可以使用此std::string
构造函数:
std::string str(u_array, 4);
so in your example:
所以在你的例子中:
std::string s(u_array, u_array+sizeof(u_array)/sizeof(u_array[0]));
回答by cpx
This should do it:
这应该这样做:
char c_array[4] = { 'a', 's', 'd', 0 };
std::string toto(array,4);
cout << toto << endl; //outputs a 3 chars and a NULL char
回答by plgDavid
There is a still a problem when the string itselfcontains a null character and you try to subsequently print the string:
当字符串本身包含空字符并且您尝试随后打印该字符串时,仍然存在问题:
cout << toto.c_str() << endl; //will only print 3 chars.
However....
然而....
std::cout.write(u_array, sizeof u_array);
std::cout << std::endl;
Its times like these when you just want to ditch cuteness and use bare C.
当你只想抛弃可爱并使用裸 C 时,就像这样的时代。
回答by Ben Hekster
Although the question was how to "copy a non null-terminated unsigned char
array [...] into a std::string
", I note that in the given example that string is only used as an input to std::cout
.
尽管问题是如何“将非空终止unsigned char
数组 [...]复制到一个std::string
”中,但我注意到在给定的示例中,该字符串仅用作std::cout
.
In that case, of course you can avoid the string altogether and just do
在这种情况下,当然您可以完全避免使用字符串而只需执行
std::string str(u_array, u_array + sizeof(u_array));
which I think may solve the problem the OP was tryingto solve.
我认为这可能会解决 OP试图解决的问题。
回答by Lightness Races in Orbit
Ew, why the cast?
呃,为什么是演员?
unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
std::string str(reinterpret_cast<char*>(u_array), sizeo(u_array));
Done.
完毕。
回答by johannes
std::string has a constructor taking an array of char and a length.
std::string 有一个构造函数,它接受一个字符数组和一个长度。
##代码##