unsigned char string in javascript and creating an equivalent buffer in c++

百般思念 提交于 2019-12-11 09:17:32

问题


I want to send a string containing unsigned characters from JavaScript to c++(using Chromium Embedded Framework).

The unsigned char at the JavaScript side would be the imageData of a Canvas element.

JavaScript code:

data = con.getImageData(0,0,mycanvas.width,mycanvas.height).data;

var imageDataString = '';
for(var i =4; i< data.length; i += 4)
{
    imageDataString+=String.fromCharCode(data[i])+ String.fromCharCode(data[i+1]) +String.fromCharCode(data[i+2]);
}
//call the native c++ function
window.cpp.func(imageDataString,mycanvas.width,mycanvas.height);

This is how i create an unsigned char buffer from the received string using c++

std::string rawString = arguments[0]->GetStringValue().ToString();
unsigned char * raw = new unsigned char[rawString.length()];
memcpy(raw, rawString.c_str(), rawString.length());

However, the image received at the c++ side does not match the image sent from JavaScript.

I have tried sending the imageData as an integer array which works but is really slow as arrays must be read index by index(using JavaScript calls) at the c++ side. The above alternative seems to be faster.

来源:https://stackoverflow.com/questions/28069853/unsigned-char-string-in-javascript-and-creating-an-equivalent-buffer-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!