将std::string转换为uint8 *类型

converting std::string to a uint8 *

本文关键字:uint8 类型 转换 std string      更新时间:2023-10-16

我有一个函数,期望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());