在 Node 中将字节数组转换为编码的 UFT8

Convert byte array to encoded UFT8 in Node

我想在 Node.js 中将字节数组转换为编码的 UFT8。在 Python 中,我使用 bytes() 方法进行转换,但是我没能在 Node 中找到类似的函数。

示例输入为 [0, 1, 66, 119, 208],理想情况下为 return \x00\x01Bw\xd0

Node.js 这些天几乎遵循浏览器标准,因此 TextDecoder https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/TextDecoder 是节点的一部分。

因此下面的代码可以在浏览器或节点中运行。

const r = new TextDecoder().decode(new Uint8Array([0, 1, 66, 119, 208]));

console.log(r);
console.log(encodeURI(r));
console.log(JSON.stringify(r));

问题是问如何将 byte 转换为 utf-8。以上就是这样做的。

OP 在与 C 字符串兼容的编码之后更多-> 这个存储库看起来应该可以解决问题.. https://github.com/harold4/node-string-escape-for-cpp/blob/master/index.js