简体   繁体   中英

converting std::string to a uint8 *

I have a function that expects a uint8 * representing a buffer and an int representing the buffer length. How should I go about transforming a std::string into a uint8 * representing the string? I have tried using c_str() to transform it into a char * but I still can't convert to uint8 *.

The function signature looks like.

InputBuffer(const UINT8* buf, int len, bool usesVarLengthStreams = true)

and I am trying to do the following

string ns = "new value";
int len = 10;

UINT8 * cbuff = ns; //(UINT8 *) ns.c_str(); doesn't work

in = new InputBuffer(cbuff, len);

string.c_str() returns a (const char *) pointer, while UINT8 is:

typedef unsigned char UINT8; 

So the code should be:

const UINT8 * cbuff = static_cast<const UINT8 *>(ns.c_str());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM