可以使用下面的代码示例来演示如何处理这个问题:
const str = "Hello World!";
const buf = Buffer.from(str);
console.log(buf);
此代码将字符串“Hello World!”转换为一个UInt8Array类型的缓冲区,并在控制台上输出该缓冲区。
为了将缓冲区还原为字符串,可以使用以下代码:
const str = "Hello World!";
const buf = Buffer.from(str);
const newStr = buf.toString();
console.log(newStr);
此代码将字符串“Hello World!”转换为一个UInt8Array类型的缓冲区,然后将其还原为一个新的字符串并在控制台上输出。
注意:要确保在使用缓冲区时处理好编码类型。如果编码类型不正确,可能会导致不可预期的结果。