const char * to vector<unsigned char> Initalisation

26,110

Solution 1

It should be

bufferType vec(buffer, buffer + size);

not

bufferType vec(buffer, size);

Solution 2

std::transform is useful for just this sort of problem. You can use it to "transform" one piece of data at a time. See documentation here:

http://www.cplusplus.com/reference/algorithm/transform/

The following code works in VS2010. (I created a std::string from your const char* array, but you could probably avoid that if you really wanted to.)

#include <algorithm>
#include <vector>

int main(int, char*[])
{
  // Initial test data
  const char* testdata = "the quick brown fox jumps over the lazy dog.";

  // Transform from 'const char*' to 'vector<unsigned char>'
  std::string input(testdata);
  std::vector<unsigned char> output(input.length());
  std::transform(input.begin(), input.end(), output.begin(),
    [](char c)
    {
      return static_cast<unsigned char>(c);
    });

  // Use the transformed data in 'output'...


  return 0;
}

Solution 3

Here is what worked for me:

// Fetch data into vector
std::vector<char> buffer = <myMethod>.getdata();

// Get a char pointer to the data in the vector
char* buf = buffer.data();

// cast from char pointer to unsigned char pointer
unsigned char* membuf = reinterpret_cast<unsigned char*>(buf);            

// now convert to vector<unsigned char> buffer
std::vector<unsigned char> vec(membuf, membuf + buffer.size()); 

// display vector<unsigned char>   
CUtils::<myMethodToShowDataBlock>(vec);      
Share:
26,110
Mr Chris
Author by

Mr Chris

Updated on January 04, 2020

Comments

  • Mr Chris
    Mr Chris over 4 years

    I understand that using vector is a good way to store binary data when using C++ and the STL. However for my unit tests I'd like to initalise the vector using a const char* C string variable.

    I'm attempting to use a variant of the code found here - Converting (void*) to std::vector<unsigned char> - to do this:

    const char* testdata = "the quick brown fox jumps over the lazy dog.";
    
    unsigned char* buffer = (unsigned char*)testdata;
    typedef vector<unsigned char> bufferType;
    
    bufferType::size_type size = strlen((const char*)buffer);
    bufferType vec(buffer, size);
    

    However the VC++ compiler is not happy with the line initialising the vector, stating:

    error C2664: 'std::vector<_Ty>::vector(unsigned int,const _Ty &)' : cannot convert parameter 1 from 'char *' to 'unsigned int'
    

    I appreciate the extreme n00bity of this question and am fully prepared for much criticism on the code above :)

    Thanks in advance, Chris

  • serup
    serup over 7 years
    I do not see how this could work if you are using large binary buffers as testdata
  • Jonathan Mee
    Jonathan Mee over 7 years
    @serup The OP says: "I'd like to initalise the vector using a const char* C string variable." I'm demonstrating how to do that. Note that we can only use a char* to represent a string because of the terminating character: '\0' A terminating character is inappropriate for binary data though, so size would have to be maintained separately from the char*. In this situation you'd need to replace the strlen(testdata) in my answer with the given size.