Copying non null-terminated unsigned char array to std::string

Posted by karlphillip on Stack Overflow See other posts from Stack Overflow or by karlphillip
Published on 2011-01-14T13:40:23Z Indexed on 2011/01/14 13:53 UTC
Read the original article Hit count: 301

Filed under:
|
|
|
|

If the array was null-terminated this would be pretty straight forward:

unsigned char u_array[4] = { 'a', 's', 'd', '\0' };
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.

© Stack Overflow or respective owner

Related posts about c++

Related posts about string