我有一个函数需要一个 uint8 * 代表一个缓冲区和一个代表缓冲区长度的 int。我应该如何将 std::string 转换为代表字符串的 uint8 *?我已经尝试使用 c_str() 将其转换为 char * 但我仍然无法转换为 uint8 *。
函数签名看起来像。
InputBuffer(const UINT8* buf, int len, bool usesVarLengthStreams = true)
我正在尝试执行以下操作
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() 返回一个 (const char *) 指针,而 UINT8 是:
typedef unsigned char UINT8;
所以代码应该是:
const UINT8 * cbuff = static_cast<const UINT8 *>(ns.c_str());
关于c++ - 将 std::string 转换为 uint8 *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23551968/