How to convert byte to string in C++?
In C++, you can use the stringstream class to convert a byte array to a string. Here is an example code:
#include <iostream>
#include <sstream>
int main() {
unsigned char bytes[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F}; // byte数组
std::stringstream ss;
for(int i = 0; i < 5; i++) {
ss << bytes[i]; // 将每个字节转换为字符
}
std::string str = ss.str(); // 将stringstream中的内容转换为string
std::cout << "Byte array converted to string: " << str << std::endl;
return 0;
}
In this example, we are converting a byte array to a string and outputting it to the console. The content and length of the byte array can be modified accordingly for the conversion.