Hello I have a vector<unsigned char*> and I want to convert it to a std::string. How can I do that?
Printable View
Hello I have a vector<unsigned char*> and I want to convert it to a std::string. How can I do that?
vector<unsigned char*> or vector<unsigned char>?
unsigned char*.
Right now I think I figured out a way, but it might not be optimal:
Code:string temp;
for(vector<unsigned char*>::iterator it = vec.begin(); it != vec.end(); it++)
temp.append(*it);
Would you like to concatenate all the char* (char arrays) being in the vector?
Yes. Is the way I posted above the best way?
It almost works yeah. There are 2 issues: append accepts a "const char*", but you are giving it an "unsigned char*", so you will have to cast that. Furthermore, the fact that you have an "unsigned char*" to begin with (and not a "char*"), seems to imply you are operating on bytes and not strings? Does your unsigned char* actually point to a null terminated array?
I don't know if it's the "best" way, but it should work yeah. If you are using C++11, you can use the simpler range-based for loop:
Code:string temp;
for (auto p : vec)
temp.append(static_cast<char*>(p));
it an "unsigned char*", so you will have to cast that. Furthermore, the fact that you have an "unsigned char*" to begin with (and not a "char*"),
it an "unsigned char*", so you will have to cast that. Furthermore, the fact that you have an "unsigned char*" to begin with (and not a "char*"),
________________
We are the ccna security training dumps in providing certkiller toefl and Mercy College exams with 100% exam pass IFPUG Download our latest The Johns Hopkins University and examsheets ccna or pass real exam of Facebook