javascript - Mozilla ctypes,从 c 数组提供 Arraybuffer

标签 javascript casting ctypes mozilla arraybuffer

我有一个带有两个参数(buffer、len)的 JavaScript 函数,其中 buffer 是长度为“len”的编码数据数组。我的函数返回两个值没有问题。但是,我不知道如何将实际数据读取到 JavaScript 字符串。 readString 对我不起作用,因为数据不是 UTF-8。

我想知道是否可以将数据(在我的例子中为“缓冲区”)提供给相同长度和相同字节大小的 ArrayBuffer

最佳答案

你的函数返回两个值?你的意思是你的c函数返回一个指向数据的指针和一个int作为长度?

假设您的 c 函数声明为

xxxx.declare('function_name',
             ctypes.default_abi, 
             ctypes.int, //the return value, suppose as length
             ctypes.uint8_t.ptr.ptr);

  var ct_ptr=new ctypes.uint8_t.ptr();
  var ct_len=function_name(ct_ptr.address());

  ct_ptr.contents is the 1st element of the array

How to access the data in javascript from the ctype pointer of type uint8_t建议 ct_ptr[1].contents 将访问第二个元素。我尝试了一下,发现[1]是 Not Acceptable 。然后我尝试转换

 var ct_array=ctypes.cast(ct_ptr, ctypes.uint8_t.array(4));
 //then ct_array[0 ...  3] is accessible.

当数组大小不超过4时确实成功。但是,它将点的值转换为字节数组。正确的做法是:

 var ct_arrayptr=ctypes.cast(ct_ptr, ctypes.uint8_t.array(ct_len.value).ptr);
 var ct_array=ct_arrayptr.contents;
 //now ct_array[0 ...  ct_len.value-1] is accessible.

关于javascript - Mozilla ctypes,从 c 数组提供 Arraybuffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20341354/

相关文章:

c++ - 数字类型之间的 C++ 转换到底是如何工作的?

python - 结构子类列表在转换为 numpy 数组时返回错误值

python - 将基于 ctypes 的代码片段从 Linux 移植到 Windows

python - : Python, ctypes.windll.user32.SystemParametersInfoA 中的参数是什么?

javascript - 对另一个 div 内的某些 div 进行编号

javascript - jQuery - 屏幕调整大小更新

java - 使用 NewInstance 进行转换

c# - 将锯齿状的 int[][] 数组转换为 object[][] 数组

javascript - 找不到全局定义的变量

javascript - 通过 javascript 或 jquery 获取多个文件值无法正常工作