如何在c ++中将字符串转换为unsigned char数组

ari*_*ies 1 c++ string

string temp;
temp = line.substr(0, pos);
Run Code Online (Sandbox Code Playgroud)

我需要将字符串temp转换为unsigned char数组.请问有人可以告诉我这是怎么做到的?我尝试了下面链接中提到的方法,但它们不起作用.

如何在visual c ++中将字符串文字转换为unsigned char数组

unsigned char* val=new[temp.size() + 1](); //Error: Expected a type.
copy(temp.begin(), temp.end(), val);
Run Code Online (Sandbox Code Playgroud)

Mar*_*ott 7

将字符串放在原始的unsigned char缓冲区中是很难看的,你不应该这样做,但这是堆栈溢出,我们必须回答.

unsigned char *val=new unsigned char[temp.length()+1];
strcpy((char *)val,temp.c_str());
Run Code Online (Sandbox Code Playgroud)