當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js Buffer.writeUInt16BE()用法及代碼示例

Buffer.writeUInt16BE()方法用於將使用Big Endian格式的指定字節寫入緩衝區對象。該值應為有效的無符號16位整數。

用法:

Buffer.writeUInt16BE( value, offset )

參數:該方法接受上述和以下所述的兩個參數:


  • value:它是一個整數值,將被寫入緩衝區。
  • offset:它是一個整數值,代表開始寫入之前要跳過的字節數,並且offset的值在0到buffer.length-2的範圍內。其默認值為0。

返回值:它返回一個整數值偏移量加上寫入的字節數。

範例1:

// Node.js program to demonstrate the   
// Buffer.writeUInt16BE() Method 
  
// Allocate a buffer 
const buf = Buffer.allocUnsafe(4); 
  
// Write the buffer element in BE format 
buf.writeUInt16BE(0xabcd, 0); 
  
// Display the buffer list 
console.log(buf); 
  
// Write the buffer element in BE format 
buf.writeUInt16BE(0xfede, 2) 
  
// Display the buffer list 
console.log(buf);

輸出:

<Buffer ab cd f4 09>
<Buffer ab cd fe de>

範例2:

// Node.js program to demonstrate the   
// Buffer.writeUInt16BE() Method 
  
// Allocate a buffer 
const buf = Buffer.allocUnsafe(4); 
  
// Write the buffer element in BE format 
buf.writeUInt16BE(0xabab, 0); 
  
// Display the buffer list 
console.log(buf); 
  
// Write the buffer element in BE format 
buf.writeUInt16BE(0xefde, 2); 
  
// Display the buffer list 
console.log(buf);

輸出:

<Buffer ab ab ad 09>
<Buffer ab ab ef de>

注意:上麵的程序將通過使用node index.js命令。

參考: https://nodejs.org/api/buffer.html#buffer_buf_writeuint16be_value_offset



相關用法


注:本文由純淨天空篩選整理自bestharadhakrishna大神的英文原創作品 Node.js | Buffer.writeUInt16BE() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。