I have a binary data in c++ variable buffer as below:
int len = 39767;
uint16_t * buffer = (uint16_t) malloc(len);
FILE * fp = fopen("rahul.jpg", "rb"); // size of file is 39767 byte.
fread(buffer, len, 1, fp);
fclose(fp);
fp = fopen("rahul3.jpg", "wb");
fwrite(buffer, len, 1, fp); // Here it is written correct.
fclose(fp);
I want to pass this buffer to Node.js and write to a file. I used below line to convert it to Local and then wrapping to an Object:
Local<String> str = Nan::New(buffer).ToLocalChecked();
return scope.Escape(str);
But, in node.js when I am checking length of received data it prints 9 only and value seems corrupted.
console.log(data);
console.log("len = " + data.length );
fs.writeFileSync('rahul2.jpg', data, 'binary');
Here rahul2.jpg is corrupted and is of 9 bytes only. How can we get rahul2.jpg from node.js code same as rahul.jpg in c++? Which Nan::New() we should use to pass binary data unaffected? Please help. Thanks.