c++ - 将 std::string 转换为 uint8 *

标签 c++

我有一个函数需要一个 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/

相关文章:

c++ - 这个makefile有什么问题

c++ - 用于将多个.cpp 和.h 编译成一个lib 的Makefile

c++ - double vector push_back 数组

c++ - htop 和 OpenMP 线程

C++ 新手 - 基本计算器问题

c++ - 在 Android Studio 下构建 Botan 时没有这样的文件 <iosfwd>?

c++ - 必须调用对非静态成员函数的引用

c++ - 我得到一个无限循环,我不确定为什么或如何修复它

c++ - 在 C++ 中调用辅助类的重写方法

c++ - 在 GTK+ 中找不到 -llintl