Use Buffer.from, as in:
...
var imgData = ctx.getImageData(0,0,250,300);
var data = imgData.data;
var buf = Buffer.from(data);
Answer from logidelic on Stack OverflowI've been able to write to stdout using the fs phantomjs module, thus being able to write as binary:
var fs = require('fs');
var stream = fs.open('/dev/stdout', 'wb');
stream.write(String.fromCharCode(128));
stream.flush();
It is still pretty slow for large volume of data, for some reason.
The only sane way of printing to the console is using console.log(). The drawback is that it inserts a newline everytime you call it, so you can't just print every character separately. You have to build a string and print it afterwards. As you noted, you can convert every byte to char with String.fromCharCode().
Also, functions like Uint8ClampedArray.prototype.join() are not yet implemented.
var a = new Uint8ClampedArray([37, 38, 39]);
console.log([].map.call(a, function(v){
return String.fromCharCode(v);
}).join());
Note that Uint8ClampedArray is not available in PhantomJS 1.x.
console.log() doesn't work in binary mode, so it will split characters above the 127 code point into two bytes when printed. This happens only when printing. You can check with
String.fromCharCode(147).length; // 1
If you are willing to write a file, you can use the fs module to write it in binary:
var a = new Uint8ClampedArray([37, 38, 39]),
fs = require('fs');
var b = [].map.call(a, function(v){
return String.fromCharCode(v);
}).join("");
fs.write("img.dat", b, "wb");