Buffer.writeUInt32LE()方法用於使用Little endian格式將指定的字節寫入緩衝區對象。該值包含有效的未分配32位整數。
用法:
Buffer.writeUInt32LE( value, offset )
參數:該方法接受上述和以下所述的兩個參數:
- value:它是一個整數值,將被寫入緩衝區。
- offset:它是一個整數值,代表開始寫入之前要跳過的字節數,並且offset的值在0到buffer.length-4的範圍內。其默認值為0。
返回值:它返回一個整數值偏移量加上寫入的字節數。
範例1:
// Node.js program to demonstrate the
// Buffer.writeUInt32LE() Method
// Allocate a buffer
const buf = Buffer.allocUnsafe(4);
// Write the buffer element in LE format
buf.writeUInt32LE(0xabcdabcd, 0);
// Display the buffer list
console.log(buf);
// Write the buffer element in LE format
buf.writeUInt32LE(0xfacedcba, 0);
// Display the buffer list
console.log(buf);
輸出:
<Buffer cd ab cd ab> <Buffer ba dc ce fa>
範例2:
// Node.js program to demonstrate the
// Buffer.writeUInt32LE() Method
// Allocate a buffer
const buf = Buffer.allocUnsafe(4);
// Write the buffer element in LE format
buf.writeUInt32LE(0xabce, 0);
// Display the buffer list
console.log(buf);
// Write the buffer element in LE format
buf.writeUInt32LE(0xeab, 0);
// Display the buffer list
console.log(buf);
輸出:
<Buffer ce ab 00 00> <Buffer ab 0e 00 00>
注意:上麵的程序將通過使用node index.js
命令。
參考: https://nodejs.org/api/buffer.html#buffer_buf_writeuint32le_value_offset
相關用法
注:本文由純淨天空篩選整理自bestharadhakrishna大神的英文原創作品 Node.js | Buffer.writeUInt32LE() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。