-
August 19th, 2014, 01:19 PM
#1
how to convert vector<unsigned char*> to std:string ?
Hello I have a vector<unsigned char*> and I want to convert it to a std::string. How can I do that?
-
August 19th, 2014, 01:41 PM
#2
Re: how to convert vector<unsigned char*> to std:string ?
vector<unsigned char*> or vector<unsigned char>?
Victor Nijegorodov
-
August 19th, 2014, 02:24 PM
#3
Re: how to convert vector<unsigned char*> to std:string ?
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);
-
August 19th, 2014, 03:00 PM
#4
Re: how to convert vector<unsigned char*> to std:string ?
Would you like to concatenate all the char* (char arrays) being in the vector?
Victor Nijegorodov
-
August 21st, 2014, 06:50 AM
#5
Re: how to convert vector<unsigned char*> to std:string ?
Yes. Is the way I posted above the best way?
-
August 21st, 2014, 07:58 AM
#6
Re: how to convert vector<unsigned char*> to std:string ?
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));
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 3rd, 2014, 01:51 PM
#7
Re: how to convert vector<unsigned char*> to std:string ?
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*"),
-
September 3rd, 2014, 01:52 PM
#8
Re: how to convert vector<unsigned char*> to std:string ?
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
Last edited by boby147; September 18th, 2014 at 11:34 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|