JavaScript:读取8个字节到64位整数 [英] JavaScript: Read 8 bytes to 64 bit integer

查看:1516
本文介绍了JavaScript:读取8个字节到64位整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含8个字节的缓冲区对象。这八个字节现在应解释为64位整数。

I have a buffer object which contains eight bytes. These eight bytes should now be interpreted as 64 bit integer.

目前我使用以下算法:

var int = buff[0];

for (var i = 1; i < buff.length; i++) {
    int += (buff[i] * Math.pow(2, 8 * i));
}

console.log(int);

这有效,但我相信有更好的方法(可能使用Uint64Array)。

this works but I believe there are better ways (maybe using Uint64Array).

很遗憾我找不到Uint16Array如何帮助我。

Unfortunately I cannot find how a Uint16Array could help me here for.

问候

更新:

// puts two 32bit integers to one 64bit integer
var bufInt = (buf.readUInt32BE(0) << 8) + buf.readUInt32BE(4);


推荐答案

Javascript不支持64位整数,因为本机数字类型是64位双精度,只给出53位整数范围。

Javascript does not support 64 bit integers, because the native number type is a 64-bit double, giving only 53 bits of integer range.

您可以创建32位数字的数组(即 Uint32Array )但如果有64位版本的那些,则无法将值复制到独立变量中。

You can create arrays of 32-bit numbers (i.e. Uint32Array) but if there were a 64-bit version of those there'd be no way to copy values from it into standalone variables.

这篇关于JavaScript:读取8个字节到64位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆