我想以大端格式将一个64位(8字节)大整数存储到Node.js缓冲区对象中。
这个任务的问题在于,Node.js缓冲区只支持将32位整数作为最大值来写入(使用buf.write32UInt32BE(value, offset))。因此,我想,为什么我们不能将64位整数拆分呢?
var buf = new Buffer(8);
buf.fill(0) // clear all bytes of the buffer
console.log(buf); // outputs <Buffer 00 00 00 00 00 00 00 00>
var int = 0xffff; // as dezimal: 65535
buf.write32UInt32BE(0xff, 4); // right the first part of the int
console.log(buf); // outputs <Buffer 00 00 00 00 00 00 00 ff>
buf.write32UInt32BE(0xff, 0); // right the second part of the int
console.log(buf); // outputs <Buffer 00 00 00 ff 00 00 00 ff>
var bufInt = buf.read32UInt32BE(0) * buf.read32UInt32BE(4);
console.log(bufInt); // outputs 65025
正如您所见,这差不多是可行的。问题仅在于分割64位整数并在读取时找到缺失的510。是否有人能够展示这两个问题的解决方案?
buf.writeBigInt64BE
。 - mouse