How to convert a string to unsigned char array in c++

c++, string

Solution

It is ugly to put a string in a raw unsigned char buffer and you should not do that, but this is stack overflow and we have to answer.

unsigned char *val=new unsigned char[temp.length()+1];
strcpy((char *)val,temp.c_str());

Problem

``` string temp; temp = line.substr(0, pos); ``` I need to convert the string temp to an unsigned char array. Could someone please tell me how this can be done? I tried the methods mentioned in the below link but they didn't work. How to convert a string literal to unsigned char array in visual c++ ``` unsigned char* val=new[temp.size() + 1](); //Error: Expected a type. copy(temp.begin(), temp.end(), val); ```

Original source

Related problems