Node.js 中的 Buffer 用于处理原始二进制数据,这在处理流、文件或网络数据时非常有用。
const buf = Buffer.from('Hello');
const buf = Buffer.alloc(10); // 10-byte buffer filled with zeros
const buf = Buffer.from([72, 101, 108, 108, 111]); // Represents 'Hello'
const buf = Buffer.from('Hello'); console.log(buf.toString()); // 'Hello'
const buf = Buffer.from('Hello'); console.log(buf.length); // 5 (each character takes 1 byte)
const buf = Buffer.alloc(5); buf.write('Hi'); console.log(buf.toString()); // 'Hi'
const buf = Buffer.from('Hello World'); const slice = buf.slice(0, 5); console.log(slice.toString()); // 'Hello'
const buf1 = Buffer.from('Hello'); const buf2 = Buffer.alloc(5); buf1.copy(buf2); console.log(buf2.toString()); // 'Hello'
const buf1 = Buffer.from('abc'); const buf2 = Buffer.from('abc'); console.log(buf1.equals(buf2)); // true
const buf1 = Buffer.from('Hello'); const buf2 = Buffer.from(' World'); const buf3 = Buffer.concat([buf1, buf2]); console.log(buf3.toString()); // 'Hello World'
以下是开始在 Node.js 中处理二进制数据时需要了解的关键 Buffer 函数:
这足以处理 Node.js 中的大多数初学者用例!
以上是缓冲区:Node.js的详细内容。更多信息请关注PHP中文网其他相关文章!