CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2013
    Posts
    32

    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?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how to convert vector<unsigned char*> to std:string ?

    vector<unsigned char*> or vector<unsigned char>?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2013
    Posts
    32

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

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  5. #5
    Join Date
    Jun 2013
    Posts
    32

    Re: how to convert vector<unsigned char*> to std:string ?

    Yes. Is the way I posted above the best way?

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  7. #7
    Join Date
    Sep 2014
    Posts
    2

    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*"),

  8. #8
    Join Date
    Sep 2014
    Posts
    2

    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
  •  





Click Here to Expand Forum to Full Width

Featured