嘿,我知道以前有人问过这个问题,但是无论出于何种原因,典型的 SO 答案对我来说都不起作用。基本上我有一个无符号字节数组:
var message = Buffer.from([
0x27, 0x52, 0x00, 0x8E
])
myAddon.test(message);
在我的 C++ 模块中,我需要将该 ByteBuffer 放入一个 const UInt8 * 数据结构中。
我目前的代码是:
#include <node.h>
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::Value;
using v8::Exception;
void test(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (!args[0] ->IsObject()) {
isolate -> ThrowException(Exception::TypeError(
v8::String::NewFromUtf8(isolate, "All arguments must be string")
));
return;
}
Local<Object> bufferObj = args[0]->ToObject();
const UInt8* d = (const UInt8*)node::Buffer::Data(bufferObj);
这只是演示问题的代码。现在发生的事情是 Buffer 是我收到的错误:命名空间“node”中没有名为“Buffer”的成员。所以当我看到人们从字节缓冲区中提取数组的例子时,我感到很困惑。是否需要导入其他头文件或库?
谢谢
最佳答案
确保您已包含 <node_buffer.h>
.否则就是正确的。
关于c++ - Node.js 6 插件将 Buffer.from() 转换为 const UInt8*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39537265/