节点缓冲区到字符数组
Node Buffer to char array
我有一个原生的 NodeJS 插件,它接受一个Buffer
实例作为它的参数之一。
我能够使用以下代码将char
数组转换为Buffer
,但正在寻找相反的方法。
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);
}
也许我迟到了,但以下代码应该可以工作:
#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
相关文章:
- 指向指向字符数组的指针数组的指针
- 比较字符数组
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 使用无符号字符数组有效存储内存
- 按字符值访问int数组
- 错误:字符数组的初始值设定项太多
- 对字符数组中的元素执行逐位操作
- C++,在int数组中输入字符串或字符会输出0,而不是ascii或error
- C++ 无法在字符数组中使用 for 循环打印字母模式
- 如何在 C++ 中从文件中读取字符数组(带有一些空格)
- 移动二维数组中的字符
- C++ 传递二维字符数组
- 无法在 C++ 中输入字符数组
- C++ 带结构的数组字符
- 数组字符包含量超出预期
- 读取文件并添加到数组字符
- 如何在Qt中更改或替换数组字符
- 为什么编译器不在参数中传递数组字符 *arr[] 的大小?
- 井字棋数组字符错误
- 分割数组字符