If the array was null-terminated this would be pretty straight forward:
unsigned char u_array[4] = { 'a', 's', 'd', '' };
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:
unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
into a std::string
.
Is there any way to do it without iterating over the unsigned char array?
Thank you all.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…