Copying non null-terminated unsigned char array to std::string
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.
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;
}
This should do it:
std::string s(u_array, u_array+sizeof(u_array)/sizeof(u_array[0]));
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);
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);
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.
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.
精彩评论