Node.js 6插件将Buffer.from()转换为const UInt8 *

时间:2016-09-16 17:37:47

标签: c++ c node.js v8 node.js-addon

嘿,我知道人们之前已经问过这个问题但无论出于何种原因,典型的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);

此唯一代码可演示此问题。现在发生的事情是缓冲区是我收到错误:命名空间'node'中没有名为'Buffer'的成员。所以当我看到人们从字节缓冲区中提取数组的例子时,我感到困惑。我需要导入另一个头文件或库吗?

由于

1 个答案:

答案 0 :(得分:1)

确保您已包含<node_buffer.h>。否则这是正确的。

另请参阅:Node Buffer to char array