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中文網其他相關文章!