Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在node.js缓冲区上从UInt64读取双精度数据?_Node.js_Date_Buffer_32bit 64bit - Fatal编程技术网

如何在node.js缓冲区上从UInt64读取双精度数据?

如何在node.js缓冲区上从UInt64读取双精度数据?,node.js,date,buffer,32bit-64bit,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), tim

我想读一个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);
var缓冲区=新缓冲区(8),
时间=日期。现在(),
//将数字转换为十六进制
var hex=时间到串(16);
而(十六进制长度<16)
十六进制='0'+十六进制;
//将数字写入UInt64
写(十六进制,0,8,'hex');
//将UInt64读取为十六进制并转换为双精度
var num=parseInt(buffer.toString('hex',0,8),16);
console.log(num==时间);