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

早过忘川 提交于 2019-12-17 10:38:20

问题


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.


回答1:


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.




回答2:


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

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



回答3:


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;
}



回答4:


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

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




回答5:


You can use this std::string constructor:

string ( const char * s, size_t n );

so in your example:

std::string str(u_array, 4);



回答6:


This should do it:

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



回答7:


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.




回答8:


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);



回答9:


Try:

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



回答10:


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));



回答11:


Ew, why the cast?

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

Done.




回答12:


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.



来源:https://stackoverflow.com/questions/4691608/copying-non-null-terminated-unsigned-char-array-to-stdstring

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