let buf1 = Buffer.alloc(6); buf1.write('hello,'); let buf2 = Buffer.alloc(6); buf2.write('世界'); let buf3 = Buffer.concat([buf1, buf2]); console.log(buf3.toString()); //如果合并后的Buffer字节数大于设置的值,则会截断 let buf4 = Buffer.concat([buf1, buf2], 9); console.log(buf4.toString());
判断是否为一个Buffer
console.log(Buffer.isBuffer({'name': 'test'})); console.log(Buffer.isBuffer(Buffer.from('test')));
通过 Buffer.byteLength() 获取字符串字节长度
console.log(Buffer.byteLength('你好,世界'));