aries
aries

Reputation: 919

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

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

Upvotes: 4

Views: 24233

Answers (1)

Martin Schlott
Martin Schlott

Reputation: 4557

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

Upvotes: 10

Related Questions