使用 << 或 >> 连接一个无符号字符数组

Using << or >> to concatenate a unsigned char array

如果我有这两个数组:

unsigned char bytes1[n];
unsigned char bytes2[m];

nm 是任意整数)我这样做:

cout << bytes1 << bytes2;

我设法显示了这 2 个数组的内容。是否可以使用这个概念来连接这两个数组?类似的东西:

usigned char bytes1[2];
unsigned char bytes2[3];
unsigned char * bytes3 = new unsigned char[5];
bytes3 << bytes1 << bytes2;

最后,bytes2应该依次是bytes1bytes2的内容。

您可能想在这种情况下使用 stringstream

此处引用: http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream

在header:

#include <sstream>

在body中:

unsigned char bytes1[2];
unsigned char bytes2[3];
unsigned char * bytes3 = new unsigned char[5];

std::stringstream ss;
ss << bytes1 << bytes2;
ss >> bytes3

//This should be also good
std::string bytes3 = ss.str();

您的原始代码不起作用,因为 byte3 不是 stream(特别是 ostream),因此它没有 << 运算符