Node.js如何使用Buffer.writeUInt32LE()方法?

2021年3月21日16:39:53 发表评论 738 次浏览

Buffer.writeUInt32LE()方法用于将使用Little Endian格式的指定字节写入缓冲区对象。该值包含有效的未分配32位整数。

语法如下:

Buffer.writeUInt32LE( 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>

注意:上面的程序将通过使用节点index.js命令。

参考: https://nodejs.org/api/buffer.html#buffer_buf_writeuint32le_value_offset


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: