Buffer.writeFloatBE()方法是Buffer模块中Buffer类的内置应用程序编程接口,用于将大端32位浮点值写入指定偏移量的分配缓冲区。
用法:
Buffer.writeFloatBE( value, offset )
参数:该方法接受上述和以下所述的两个参数:
- value:此参数指定要写入缓冲区的4字节浮点值。它应该是有效的32位大字节序浮点值。当值不是此值时,行为是不确定的。
- offset:它指定在写入之前要跳过的字节数,或者只是表示缓冲区中的索引。 offset的值为0
返回值:此方法返回一个整数值,该值是偏移量和以big endian格式写入的字节数的总和。
以下示例说明了Node.js中Buffer.writeFloatBE()方法的使用:
范例1:
// Node.js program to demonstrate the
// Buffer.writeFloatBE() method
// Allocating 16bytes buffer
const buf = Buffer.allocUnsafe(16);
// Writing 32bit or 4 byte floating point
// values to the buffer and printing
// returned value to console
console.log(buf.writeFloatBE(0xbabeface, 0));
console.log(buf.writeFloatBE(0x00000000, 4));
console.log(buf.writeFloatBE(0xffffffff, 8));
console.log(buf.writeFloatBE(0xcabcbcbc, 12));
// Printing the buffer
console.log(buf);
输出:
4 8 12 16 <Buffer 4f 3a be fb 00 00 00 00 4f 80 00 00 4f 4a bc bd>
范例2:
// Node.js program to demonstrate the
// Buffer.writeFloatBE() method
// Allocating 8bytes buffer
const buf = Buffer.allocUnsafe(8);
// Printing buffer before writing value
console.log("Before writing into buffer:");
console.log(buf);
// Writing 32bits or 4 bytes floating
// point values to the buffer and
// printing returned value to console
console.log(buf.writeFloatBE(0xbabebabe, 0));
console.log(buf.writeFloatBE(0xdecade20, 4));
// Printing the buffer after writing into buffer
console.log("After writing into buffer:");
console.log(buf);
输出:
Before writing into buffer: <Buffer 00 00 00 00 00 00 00 00> 4 8 After writing into buffer: <Buffer 4f 3a be bb 4f 5e ca de>
注意:上面的程序将通过使用node index.js
命令。
参考: https://nodejs.org/api/buffer.html#buffer_buf_writefloatbe_value_offset
相关用法
注:本文由纯净天空筛选整理自gekcho大神的英文原创作品 Node.js | Buffer.writeFloatBE() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。