gpt4 book ai didi

node.js - 如何从 Node.js 缓冲区上的 UInt64 读取 Double?

转载 作者:太空宇宙 更新时间:2023-11-03 21:59:56 26 4
gpt4 key购买 nike

我想读取 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);

关于node.js - 如何从 Node.js 缓冲区上的 UInt64 读取 Double?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29503541/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com