緩衝區是一種臨時存儲器,用於在將數據從一個位置移動到另一位置時存儲數據。它就像一個整數數組。
Buffer.writeIntLE()方法使用Little Endian格式將值的byteLength字節寫入Buffer到Buffer對象。
用法:
Buffer.writeIntLE( value, offset, byteLength )
參數:此方法接受上述和以下所述的三個參數:
- value:它包含一個要寫入緩衝區的整數值。
- offset:它擁有一個整數值,即開始寫入緩衝區之前要跳過的字節數。 offset的值為0
- byteLength:它保存要寫入緩衝區的字節數。 byteLength的值為0
返回值:它返回一個整數值偏移量加上寫入的字節數。
範例1:
// Node program to demonstrate the
// Buffer.readInt16LE() Method
// Allocating buffer from array
var buf = Buffer.from('GeeksForGeeks');
buf.writeIntLE('ee', 0, 5);
// Printing allocated buffer
console.log(buf);
console.log(buf.toString());
輸出:
<Buffer 00 00 00 00 00 46 6f 72 47 65 65 6b 73> ForGeeks
範例2:
// Node program to demonstrate the
// Buffer.readInt16LE() Method
// Allocating buffer from array
const buf = Buffer.allocUnsafe(4);
buf.writeIntLE(0xabcdef, 0, 4);
// Printing allocated buffer
console.log(buf);
輸出:
<Buffer ef cd ab 00>
注意:上麵的程序將通過使用node index.js
命令。
參考: https://nodejs.org/api/buffer.html#buffer_buf_writeintle_value_offset_bytelength
相關用法
注:本文由純淨天空篩選整理自08shubhambhandari08大神的英文原創作品 Node.js | Buffer.writeIntLE() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。