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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。