[英]Copying non null-terminated unsigned char array to std::string
If the array was null-terminated this would be pretty straight forward: 如果数组是以null结尾的,那么这将非常简单:
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: 但是,我想知道复制非null终止的 unsigned char数组的最合适方法是什么,如下所示:
unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
into a std::string
. 到std::string
。
Is there any way to do it without iterating over the unsigned char array ? 有没有办法在没有迭代unsigned char数组的情况下做到这一点?
Thank you all. 谢谢你们。
std::string
has a constructor that takes a pair of iterators and unsigned char
can be converted (in an implementation defined manner) to char
so this works. std::string
有一个构造函数 ,它接受一对迭代器, unsigned char
可以转换(以实现定义的方式)为char
这样就可以了。 There is no need for a reinterpret_cast
. 不需要reinterpret_cast
。
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;
}
Of course an "array size" template function is more robust than the sizeof
calculation. 当然“阵列大小”模板函数比更健壮sizeof
计算。
好吧,显然std :: string有一个可以在这种情况下使用的构造函数 :
std::string str(reinterpret_cast<char*>(u_array), 4);
When constructing a string without specifying its size, constructor will iterate over aa character array and look for null-terminator, which is '\\0'
character. 在构造字符串而不指定其大小时,构造函数将遍历一个字符数组并查找null-terminator,即'\\0'
字符。 If you don't have that character, you have to specify length explicitly, for example: 如果您没有该字符,则必须明确指定长度,例如:
// --*-- 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;
}
std::string has a method named assign. std :: string有一个名为assign的方法。 You can use a char * and a size. 您可以使用char *和大小。
http://www.cplusplus.com/reference/string/string/assign/ http://www.cplusplus.com/reference/string/string/assign/
You can use this std::string
constructor: 你可以使用这个std::string
构造函数:
string ( const char * s, size_t n );
so in your example: 所以在你的例子中:
std::string str(u_array, 4);
这应该这样做:
std::string s(u_array, u_array+sizeof(u_array)/sizeof(u_array[0]));
There is a still a problem when the string itself contains a null character and you try to subsequently print the string: 当字符串本身包含空字符并且您尝试随后打印字符串时仍然存在问题:
char c_array[4] = { 'a', 's', 'd', 0 };
std::string toto(array,4);
cout << toto << endl; //outputs a 3 chars and a NULL char
However.... 然而....
cout << toto.c_str() << endl; //will only print 3 chars.
Its times like these when you just want to ditch cuteness and use bare C. 当你只是想放弃可爱并使用裸露的C.
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: 从而:
std::string str(&u_array[0], &u_array[0] + 4);
Try: 尝试:
std::string str;
str.resize(4);
std::copy(u_array, u_array+4, str.begin());
std::string has a constructor taking an array of char and a length. std :: string有一个构造函数,它接受一个char数组和一个length。
unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
std::string str(reinterpret_cast<char*>(u_array), sizeo(u_array));
Ew, why the cast? 呃,演员为什么?
std::string str(u_array, u_array + sizeof(u_array));
Done. 完成。
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::cout.write(u_array, sizeof u_array);
std::cout << std::endl;
which I think may solve the problem the OP was trying to solve. 我认为可以解决OP 试图解决的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.