how best to combine multiple vector<unsigned char> to std::string?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: how best to combine multiple vector<unsigned char> to std::string?

  1. #1
    Join Date
    Mar 2009
    Posts
    166

    how best to combine multiple vector<unsigned char> to std::string?

    Hello all,

    two questions:

    1. I have some vector<unsigned char> containing binary data. I would like to combine them into one std::string. How is the correct way to accomplish this?

    This is my best guess for sample code:
    Code:
    vector<unsigned char> data; //conatins some data
    vector<unsigned char> data2; //contains more data
    string temp(data.begin(), data.end());
    temp.append(data2.begin(), data2.end());
    will this code work with binary data, or will it null terminate?


    2. A similiar problem.. I have some unsigned char* variables, and I want to combine them into one std::string. How can I accomplish this?

    will the member append() work here? or will it null terminate?

    something like:
    Code:
    unsigned char* data; //conatins some data
    unsigned char* data2; //contains more data
    string temp(reinterpret_cast<const char*>(data));
    temp.append(string(reinterpret_cast<const char*>(data2)));
    will the above sample code work without null termination?

    Regards,
    Ellay K.
    Last edited by ekhule; March 16th, 2012 at 10:58 AM.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: how best to combine multiple vector<unsigned char> to std::string?

    First of all, if the unsigned char* data is not null-terminated, then you are doomed anyway unless you also know the length (not shown above). So the function you use will definitely need to take either the length or an end iterator as a parameter if you are not assuming null-termination. The good news is that any function which takes an end iterator or a length will usually not assume null-termination (check the docs to be sure).

    Fortunately, there are overloads of append() which suit both cases. One overload takes a const char* data pointer and a size_t length, which can be used with your unsigned char* data with a simple cast. The other takes two iterators, which can be easily used with the vectors.

    For efficiency, you should consider using reserve() to first allocate enough space for all the strings. This isn't necessary for correctness though.

  3. #3
    Join Date
    Mar 2009
    Posts
    166

    Re: how best to combine multiple vector<unsigned char> to std::string?

    Ah ok, yeah I know the length.. modified code:

    Code:
    string temp;
    temp.reserve(totallength);
    unsigned char* data; //conatins some data
    unsigned char* data2; //contains more data
    temp=string(reinterpret_cast<const char*>(data), lengthdata1);
    temp.append(string(reinterpret_cast<const char*>(data2), lengthdata2));
    Does this look correct? optimal?

    Thanks,
    Ellay K.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: how best to combine multiple vector<unsigned char> to std::string?

    As a general rule, you should prefer static_cast to reinterpret_cast. It will usually allow you to do anything valid and prevent you from accidentally doing something really wacky, whereas reinterpret_cast is far less constrained. A static_cast to char* should work. (const char* might not, since I don't think static_cast allows changing constness, but that isn't required here anyway.)

    Alternatively, no cast is required at all if you use the form of append taking two iterators. Recall that a pointer is a special case of an iterator and how pointer arithmetic works, and you can just do:
    Code:
    temp.append(data, data + lengthdata1);
    temp.append(data2, data2 + lengthdata2);
    There should be no difference in performance, it's just a matter of code style.

  5. #5
    Join Date
    Mar 2009
    Posts
    166

    Re: how best to combine multiple vector<unsigned char> to std::string?

    Thanks so much, I appreciate your time today in helping me!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center