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

标签 node.js date buffer 32bit-64bit

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

相关文章:

c++ - 将字符串格式化为日期

java - 内部文件服务器设计模式?

javascript - 使用 Node.js 从 POST 请求中唯一标识用户

php - 触发 Node js文件

php - 防止 PHP date() 默认为 12/31/1969

PHP:在特定日期自动更新数据库字段,没有通过 cronjob 的脚本

angularjs - MEAN.JS 新 Controller 功能和路线

linux - 让linux缓冲/dev/random

php - 同时停止并返回输出缓冲

Java 来自套接字 NIO ByteBuffer 的部分消息处理