节点缓冲区到char数组 [英] Node Buffer to char array
本文介绍了节点缓冲区到char数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个本地NodeJS插件,它接受一个 Buffer
实例作为其参数之一。
I have a native NodeJS addon that accepts a Buffer
instance as one of it's arguments.
可以使用以下代码将 char
数组转换为缓冲区
,但要寻找其他方法。 / p>
I'm able to convert a char
array into a Buffer
with the following code, but looking for the other way around.
static v8::Local<v8::Object> create_buffer(char *data, unsigned long length) {
node::Buffer *slow_buffer = node::Buffer::New(length);
memcpy(node::Buffer::Data(slow_buffer), data, length);
v8::Handle<v8::Value> constructor_arguments[3] = {
slow_buffer->handle_,
v8::Integer::New(length),
v8::Integer::New(0)
};
v8::Local<v8::Object> global_object = v8::Context::GetCurrent()->Global();
v8::Local<v8::Function> buffer_constructor = v8::Local<v8::Function>::Cast(global_object->Get(v8::String::New("Buffer")));
return buffer_constructor->NewInstance(3, constructor_arguments);
}
推荐答案
但下面的代码应该工作:
Maybe I'm late, but the following code should work:
#include <node.h>
#include <node_buffer.h>
void Test(const FunctionCallbackInfo<Value>& args)
{
Local<Object> bufferObj = args[0]->ToObject();
char* bufferData = node::Buffer::Data(bufferObj);
size_t bufferLength = node::Buffer::Length(bufferObj);
}
参考:
- http://www.puritys.me/docs-blog/article-286-How-to-pass-the-paramater- of-Node.js-or-io.js-into-native-C / C ++ - function..html
- https://github.com/nodejs/nan/blob/master/nan.h#L25
- http://www.puritys.me/docs-blog/article-286-How-to-pass-the-paramater-of-Node.js-or-io.js-into-native-C/C++-function..html
- https://github.com/nodejs/nan/blob/master/nan.h#L25
这篇关于节点缓冲区到char数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文