unsigned char string in javascript and creating an

2019-08-03 15:06发布

问题:

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.