I'm looking to convert a byte array to encoded UFT8 in Node. In Python, I've used the bytes() method to convert however I haven't been able to find a similar function in Node.
An example input would be [0, 1, 66, 119, 208] which would ideally return \x00\x01Bw\xd0
Node.js these days pretty much follows browser standards, as such the TextDecoder https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/TextDecoder is part of node.
So this code below will work in browser or node.
const r = new TextDecoder().decode(new Uint8Array([0, 1, 66, 119, 208]));
console.log(r);
console.log(encodeURI(r));
console.log(JSON.stringify(r));
The question was asking for how to convert byte to utf-8. The above does this.
The OP was more after a encoding compatible with C strings-> This repository looks like it should do the trick.. https://github.com/harold4/node-string-escape-for-cpp/blob/master/index.js