如何从 node.js 缓冲区上的 UInt64 读取 Double?
How to read Double from UInt64 on node.js Buffer?
我想读取一个 UInt64BE 并将其转换为 Double。
我该怎么做?
我将 Double 转换为 UInt64BE 如下:
var time = Date.now();
buffer.writeUInt32BE(parseInt(time / 0xffffffff, 10), 0);
buffer.writeUInt32BE(parseInt(time & 0xffffffff, 10), 4);
我找到了以下解决方案:
var buffer = new Buffer(8),
time = Date.now(),
// convert number to hex
var hex = time.toString(16);
while(hex.length < 16)
hex = '0' + hex;
// write number as UInt64
buffer.write(hex, 0, 8, 'hex');
// read UInt64 as hex and convert to Double
var num = parseInt(buffer.toString('hex', 0, 8), 16);
console.log(num === time);
我想读取一个 UInt64BE 并将其转换为 Double。 我该怎么做?
我将 Double 转换为 UInt64BE 如下:
var time = Date.now();
buffer.writeUInt32BE(parseInt(time / 0xffffffff, 10), 0);
buffer.writeUInt32BE(parseInt(time & 0xffffffff, 10), 4);
我找到了以下解决方案:
var buffer = new Buffer(8),
time = Date.now(),
// convert number to hex
var hex = time.toString(16);
while(hex.length < 16)
hex = '0' + hex;
// write number as UInt64
buffer.write(hex, 0, 8, 'hex');
// read UInt64 as hex and convert to Double
var num = parseInt(buffer.toString('hex', 0, 8), 16);
console.log(num === time);