[英]What is the UTF8ToString equivalent for turning binary data to a Javascript array
I have C code which returns a void * and size_t length to Javascript running in a web worker.我有 C 代码,它返回一个void * 和size_t长度到 Javascript 在 web 工作人员中运行。 In Javascript i have the following
在 Javascript 我有以下
let start = getStartAddress();
let len = getLength();
I know for Strings i can do我知道我可以做的字符串
let mystring = UTF8ToString(start);
This is documented https://emscripten.org/docs/api_reference/preamble.js.html#conversion-functions-strings-pointers-and-arrays这是记录在 https://emscripten.org/docs/api_reference/preamble.js.html#conversion-functions-strings-pointers-and-arrays
I am at a total loss as to how i get an Uint8Array that i can send through postMessage .我完全不知道如何获得可以通过postMessage发送的Uint8Array 。
Where would i find documentation that tells me how to do that?我在哪里可以找到告诉我如何做到这一点的文档?
In the absence of documentation an answer to my question would suffice.在没有文件的情况下,我的问题的答案就足够了。
Ok, so i actually found the definition for UTF8ToString in the Javascript file the emscripten compiler created for my .wasm
file.好的,所以我实际上在 emscripten 编译器为我的
.wasm
文件创建的 Javascript 文件中找到了UTF8ToString的定义。 I thought it was a native function.我以为是原生的 function。 It calls UTF8ArrayToString in the same javascript file.
它在同一个 javascript 文件中调用UTF8ArrayToString 。 Learning from that source code i came up with the following.
从该源代码中学习我想出了以下内容。
function BytesToArray(base, len) {
let out = new Array(len);
let i = 0, ptr = base, end = base + len;
while (ptr < end) {
out[i] = HEAPU8[ptr];
i++;
ptr++;
}
return out;
}
let mydata = BytesToArray(start, len);
postMessage(mydata);
I hope the performance isn't too bad.我希望表现不要太差。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.