阅读量:114
在C++中,可以使用stringstream类来将byte数组转换为string。下面是一个示例代码:
#include
#include
int main() {
unsigned char bytes[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F}; // byte数组
std::stringstream ss;
for(int i = 0; i < 5; i++) {
ss << bytes class="hljs-comment">// 将每个字节转换为字符
}
std::string str = ss.str(); // 将stringstream中的内容转换为string
std::cout << "Byte array converted to string: " << str class="hljs-keyword">return 0;
}
在这个例子中,我们将一个byte数组转换为string,并将其输出到控制台。可以根据实际情况修改byte数组的内容和长度来进行转换。