Node.js 6增加了将Buffer.from()转换为const UInt8*的功能



嘿,我知道以前有人问过这个问题,但是出于某种原因,典型的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.h>

参见:Node Buffer to char array

最新更新