当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js Buffer.writeDoubleBE()用法及代码示例


Buffer.writeDoubleBE()方法是Buffer模块中Buffer类的内置应用程序编程接口,用于将Big Endian 64位double值以指定的偏移量写入分配的缓冲区。

用法:

Buffer.writeDoubleBE( value, offset )

参数:该方法接受上述和以下所述的两个参数:


  • value:此参数指定要写入缓冲区的4字节浮点值。它应该是有效的64位大字节序双精度值。当值不是此值时,行为是不确定的。
  • offset:它指定在写入之前要跳过的字节数,或者只是表示缓冲区中的索引。 offset的值为0

返回值:此方法返回一个整数值,该值是偏移量和写入的字节数的总和。

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

范例1:

// Node.js program to demonstrate the  
// Buffer.writeDoubleBE() method  
       
// Allocating 32 bytes buffer 
const buf = Buffer.allocUnsafe(32); 
  
// Writing 64bits or 8 bytes double values 
// to the buffer and printing returned 
//  value to console 
console.log(buf.writeDoubleBE(123.123, 0)); 
console.log(buf.writeDoubleBE(166.089, 8)); 
console.log(buf.writeDoubleBE(231.678, 16)); 
console.log(buf.writeDoubleBE(341.781, 24)); 
   
// Display the buffer 
console.log(buf);

输出:

8
16
24
32
<Buffer 40 5e c7 df 3b 64 5a 1d 40 64 c2 d9 16 87 2b
02 40 6c f5 b2 2d 0e 56 04 40 75 5c 7e f9 db 22 d1>

范例2:

// Node.js program to demonstrate the  
// Buffer.writeDoubleBE() method  
       
// Allocating 16 bytes buffer 
const buf = Buffer.allocUnsafe(16); 
  
// Printing buffer before writing value 
console.log("Before writing into buffer:"); 
console.log(buf); 
   
// Writing 64 bits or 8 bytes double values 
// to the buffer and printing returned 
// value to console 
console.log(buf.writeDoubleBE(219.098, 0)); 
console.log(buf.writeFloatBE(169.096, 8)); 
   
// Printing the buffer after writing into buffer 
console.log("After writing into buffer:"); 
console.log(buf);

输出:

Before writing into buffer:
<Buffer f8 02 ff bc f8 01 00 00 f8 02 ff bc f8 01 00 00>
8
12
After writing into buffer:
<Buffer 40 6b 63 22 d0 e5 60 42 43 29 18 93 f8 01 00 00>

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

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



相关用法


注:本文由纯净天空筛选整理自gekcho大神的英文原创作品 Node.js | Buffer.writeDoubleBE() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。