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

此生再无相见时 提交于 2019-11-27 19:16:36

std::string has a constructor that takes a pair of iterators and unsigned char can be converted (in an implementation defined manner) to char so this works. There is no need for a reinterpret_cast.

unsigned char u_array[4] = { 'a', 's', 'd', 'f' };

#include <string>
#include <iostream>
#include <ostream>

int main()
{
    std::string str( u_array, u_array + sizeof u_array / sizeof u_array[0] );
    std::cout << str << std::endl;
    return 0;
}

Of course an "array size" template function is more robust than the sizeof calculation.

Well, apparently std::string has a constructor that could be used in this case:

std::string str(reinterpret_cast<char*>(u_array), 4);

When constructing a string without specifying its size, constructor will iterate over a a character array and look for null-terminator, which is '\0' character. If you don't have that character, you have to specify length explicitly, for example:

// --*-- C++ --*--

#include <string>
#include <iostream>


int
main ()
{
    unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
    std::string str (reinterpret_cast<const char *> (u_array),
                     sizeof (u_array) / sizeof (u_array[0]));
    std::cout << "-> " << str << std::endl;
}

std::string has a method named assign. You can use a char * and a size.

http://www.cplusplus.com/reference/string/string/assign/

You can use this std::string constructor:

string ( const char * s, size_t n );

so in your example:

std::string str(u_array, 4);
cpx

This should do it:

std::string s(u_array, u_array+sizeof(u_array)/sizeof(u_array[0]));
plgDavid

There is a still a problem when the string itself contains a null character and you try to subsequently print the string:

char c_array[4] = { 'a', 's', 'd', 0 };

std::string toto(array,4);
cout << toto << endl;  //outputs a 3 chars and a NULL char

However....

cout << toto.c_str() << endl; //will only print 3 chars.

Its times like these when you just want to ditch cuteness and use bare C.

You can create a character pointer pointing to the first character, and another pointing to one-past-the-last, and construct using those two pointers as iterators. Thus:

std::string str(&u_array[0], &u_array[0] + 4);

Try:

std::string str;
str.resize(4);
std::copy(u_array, u_array+4, str.begin());

std::string has a constructor taking an array of char and a length.

unsigned char u_array[4] = { 'a', 's', 'd', 'f' };
std::string str(reinterpret_cast<char*>(u_array), sizeo(u_array));

Ew, why the cast?

 std::string str(u_array, u_array + sizeof(u_array));

Done.

Although the question was how to "copy a non null-terminated unsigned char array [...] into a std::string", I note that in the given example that string is only used as an input to std::cout.

In that case, of course you can avoid the string altogether and just do

std::cout.write(u_array, sizeof u_array);
std::cout << std::endl;

which I think may solve the problem the OP was trying to solve.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!