I tried to convert unsigned char array to an std::string of size 16. But I don`t know why result string size is different. I want to know the reason.
#include <iostream>
using namespace std;
const unsigned char t[16] = { 0x1E, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 ,0x08 ,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 };
const unsigned char t2[16] = { 0x1E, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 ,0x08 ,0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 };
const unsigned char t3[16] = { 0x44, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 ,0x68 ,0x69, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65 };
int main() {
string a, a2, a3;
a = static_cast<std::string>(reinterpret_cast<const char*>(t));
a2.assign(reinterpret_cast<const char*>(t2), 16);
a3.assign(reinterpret_cast<const char*>(t3));
cout << "size : " << a.size() << endl;
for (int i = 0; i < a.size(); i++)
printf("0x%02X ", a.c_str()[i]);
cout << endl << endl;
cout << "size2 : " << a2.size() << endl;
for (int i = 0; i < a2.size(); i++)
printf("0x%02X ", a2.c_str()[i]);
cout << endl << endl;
cout << "size3 : " << a3.size() << endl;
for (int i = 0; i < a3.size(); i++)
printf("0x%02X ", a3.c_str()[i]);
cout << endl << endl;
return 0;
}
result
size : 56
0x1E 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x10 0x11 0x12 0x13 0x14 0x15 0x1E 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x10 0x11 0x12 0x13 0x14 0x15 0x44 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x68 0x69 0x60 0x61 0x62 0x63 0x64 0x65 0x5F 0x41 0x72 0x67 0x4C 0x69 0x73 0x74
size2 : 16
0x1E 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x10 0x11 0x12 0x13 0x14 0x15
size3 : 24
0x44 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0x68 0x69 0x60 0x61 0x62 0x63 0x64 0x65 0x5F 0x41 0x72 0x67 0x4C 0x69 0x73 0x74
(6):Replaces the contents with those of null-terminated character string pointed to by s. Is your input null-terminated character string?