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


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

Buffer.writeBigUInt64BE()方法是Buffer模塊中Buffer類的內置應用程序編程接口,用於將Big Endian 64位Big整數值以指定的偏移量寫入分配的緩衝區。

用法:

Buffer.writeBigUInt64BE( value, offset )

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

  • value:此參數指定要寫入緩衝區的大整數值。它應該是有效的64位big endian Big整數值。當值不是此值時,行為是不確定的。
  • offset:它指定在寫入之前要跳過的字節數,或者隻是表示緩衝區中的索引。 offset的值是0 <= offset <= Buffer.length-8。其默認值為0。

返回值:此方法返回一個無符號整數值,該值是偏移量和寫入的字節數的總和。

以下示例說明了Node.js中Buffer.writeBigUInt64BE()方法的使用:



範例1:
文件名:index.js

// Node.js program to demonstrate the 
// buffer.writeBigUInt64BE() method  
const buf = Buffer.allocUnsafe(8); 
  
// wWiting big integer value into buffer 
// by using writeBigUInt64BE() method 
buf.writeBigUInt64BE(0x01030405060708n, 0); 
  
// Display the buffer 
console.log(buf);

使用以下命令運行index.js文件:

node index.js

輸出:

<Buffer 00 01 03 04 05 06 07 08>

範例2:
文件名:index.js

// Node.js program to demonstrate the 
// buffer.writeBigUInt64BE() method  
const buf = Buffer.allocUnsafe(8); 
  
// writing big integer value into buffer 
// by using writeBigUInt64BE() method 
buf.writeBigUInt64BE(0xaa03040506efffn, 0); 
  
// display the buffer 
console.log(buf);

使用以下命令運行index.js文件:

node index.js

輸出:

<Buffer 00 aa 03 04 05 06 ef ff>

參考: https://nodejs.org/dist/latest-v12.x/docs/api/buffer.html#buffer_buf_writebiguint64be_value_offset

相關用法


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