Buffer.writeInt16LE()方法是Buffer模塊中Buffer類的內置應用程序編程接口,用於編寫有效的帶符號16位整數,以指定的偏移量以指定的小端字節序格式進行緩存。
用法:
Buffer.writeInt16LE( value, offset )
參數:該方法接受上述和以下所述的兩個參數:
- value:要寫入緩衝區的有效帶符號的16位整數。
- offset:它表示開始寫入緩衝區之前要跳過的字節數。 offset的值在0範圍內
返回值:它以小字節序格式返回一個緩衝區,該緩衝區的值以指定的偏移量插入。
以下示例說明了Node.js中Buffer.writeInt16LE()方法的使用:
範例1:
// Node.js program to demonstrate the
// Buffer.writeInt16LE() method
// Creating a buffer
const buf = Buffer.allocUnsafe(10);
// Using Buffer.writeInt16LE() method
buf.writeInt16LE(0x0114, 0);
// Display the buffer
console.log(buf);
// Using Buffer.writeInt16LE() method
buf.writeInt16LE(0x1015, 2);
// Display the buffer
console.log(buf);
// Using Buffer.writeInt16LE() method
buf.writeInt16LE(0x0016, 8);
// Display the buffer
console.log(buf);
// Using Buffer.writeInt16LE() method
buf.writeInt16LE(0x0217, 6);
// Display the buffer
console.log(buf);
// Using Buffer.writeInt16LE() method
buf.writeInt16LE(0x0518, 4);
// Display the result
console.log(buf);
輸出:
<Buffer 14 01 9b 02 00 00 00 00 a8 79> <Buffer 14 01 15 10 00 00 00 00 a8 79> <Buffer 14 01 15 10 00 00 00 00 16 00> <Buffer 14 01 15 10 00 00 17 02 16 00> <Buffer 14 01 15 10 18 05 17 02 16 00>
範例2:
// Node.js program to demonstrate the
// Buffer.writeInt16LE() method
// Creating a buffer
const buf = Buffer.allocUnsafe(6);
// Using Buffer.writeInt16LE() method
buf.writeInt16LE(0x2211, 0);
// Display the buffer
console.log(buf);
// Using Buffer.writeInt16LE() method
buf.writeInt16LE(0x4433, 2);
// Display the buffer
console.log(buf);
// Using Buffer.writeInt16LE() method
buf.writeInt16LE(0x6655, 4);
// Display the buffer
console.log(buf);
輸出:
<Buffer 11 22 00 00 00 00> <Buffer 11 22 33 44 00 00> <Buffer 11 22 33 44 55 66>
注意:上麵的程序將通過使用node index.js
命令。
參考: https://nodejs.org/dist/latest-v13.x/docs/api/buffer.html#buffer_buf_writeint16le_value_offset
相關用法
注:本文由純淨天空篩選整理自Rajnis09大神的英文原創作品 Node.js | Buffer.writeInt16LE() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。