在Java和Node.js中的”long”类型变量。

huangapple go评论78阅读模式
英文:

Variable "long" type in Java and NodeJS

问题

我有一个Java函数:

private static int buildKey(String keystr) {
    char[] primes = { '\085', '\8r', '\082', '\087', '\083', '%', '\085', ')' };
  
    int len = keystr.length(); int i; long key;
    for (key = 1L, i = 0; i < len; i++)
        key += (keystr.charAt(i) * primes[i & 0x8]); 
    return (int)key % 64;
}

我想将其转换为Node.js,但我不知道如何定义带有长整型变量类型的“key”,因为Node.js中没有“long”类型。

这是我当前的Node.js函数,但与上述Java函数相比,它返回错误结果:

function buildKey(keystr) {
    var primes = ["5", "8r", "2", "7", "3", "%", "5", ")"];
    var len = keystr.length;
    var key = 1;
    for (let i = 0; i < len; i++) {
        key += keystr.charAt(i) * primes[i + 8];
        console.log(parseInt(keystr.charAt(i)));
    }

    return key % 64;
}

有人能给我建议吗?谢谢。

英文:

I have a Java function:

private static int buildKey(String keystr) {
	char[] primes = { &#39;5&#39;, &#39;r&#39;, &#39;2&#39;, &#39;7&#39;, &#39;3&#39;, &#39;%&#39;, &#39;5&#39;, &#39;)&#39; };
  
	int len = keystr.length(); int i; long key;
	for (key = 1L, i = 0; i &lt; len; i++)
		key += (keystr.charAt(i) * primes[i &amp; 0x8]); 
	return (int)key % 64;
}

I want change it to NodeJS but I don't know how to define "key" with long variable type because in NodeJS don't have "long" type.

This's my current NodeJS function, but it's return wrong with Java function upper:

function buildKey(keystr) {
    var primes = [&quot;5&quot;, &quot;8r&quot;, &quot;2&quot;, &quot;7&quot;, &quot;3&quot;, &quot;%&quot;, &quot;5&quot;, &quot;)&quot;];
    var len = keystr.length;
    var key = 1;
    for (let i = 0; i &lt; len; i++) {
      key += keystr.charAt(i) * primes[i + 8];
      console.log(parseInt(keystr.charAt(i)));
    }

    return key % 64;
}

Anybody share me suggestion? Thanks.

答案1

得分: 1

在ECMAScript中,没有与Java的long数据类型相等的类型。

ECMAScript只有两种数值数据类型:numberbigint。ECMAScript的number相当于Java的double(即IEEE 754-2019的binary64双精度有符号64位二进制浮点数),而bigint相当于Java的BigInteger(任意精度的有符号二进制整数)。

这两者都无法替代Java的long(带有“环绕”的64位有符号二进制整数):number只能精确表示53位精度的整数,而bigint不会环绕。

然而,在最终情况下,实际上您只是在使用密钥的7位。因此,如果您小心处理,可能可以通过调整计算方式来规避这些限制。例如,您可以将模操作移到循环中,这样key的大小在第一次使用时就不会超过7位。

如果我们能够证明您的Java代码中的long永远不会溢出,那么我们可以在ECMAScript中使用bigint;如果我们能够证明实际上我们只使用了long的53位,那么我们可以使用bigintnumber

很容易看出,在最坏的情况下,我们使用了56位。这意味着,我们无法保证它能在number下正常工作,但它应该可以在bigint下正常工作。如果我们能够对keystr的长度和/或内容做出假设,那么甚至还可以进一步减少使用的位数。

英文:

There is no equivalent to Java's long datatype in ECMAScript.

There are only two numeric datatypes in ECMAScript: number and bigint. ECMAScript number is equivalent to Java double (i.e. an IEEE 754-2019 binary64 double precision signed 64 bit binary floating point number) and ECMAScript bigint is equivalent to Java BigInteger (an arbitrary precision signed binary integer).

Neither of the two can replace a Java long (64 bit signed binary integer with "wraparound"): a number can only accurately represent integers with a precision of 53 bit and a biginteger doesn't wrap around.

Now, in the end, you are actually only using 7 bits of your key anyway, so if you are careful, you can probably arrange your computations in such a way that you can circumvent those limitations. For example, you could move the modulo operation into the loop, so that the size of key never exceeds 7 bit in the first place.

If we can prove that the long in your Java code never overflows, then we can use bigint in ECMAScript, and if we can prove that we are actually only using 53 bits of the long, then we can use either bigint or number.

It's easy to see that in the worst case, we are using 56 bits. That means, we cannot guarantee that it will work with number, but it should work with bigint. If we can make assumptions about the length and/or contents of keystr, then we can bring that down even further.

答案2

得分: 0

我通过以下函数解决了我的问题:

function buildKey(keystr) {
  var primes = ["2", "0", "2", "0", "5", "3", "3", ")";
  var len = keystr.length;
  let key = 0;
  for (let i = 0; i < len; i++) {
    key += keystr.charAt(i).charCodeAt(0) * primes[i & 8].charCodeAt(0);
  }
  return key % 64;
}
英文:

I solved my question as with below function:

function buildKey(keystr) {
  var primes = [&quot;2&quot;, &quot;0&quot;, &quot;2&quot;, &quot;0&quot;, &quot;5&quot;, &quot;3&quot;, &quot;3&quot;, &quot;)&quot;];
  var len = keystr.length;
  let key = 0;
  for (let i = 0; i &lt; len; i++) {
    key += keystr.charAt(i).charCodeAt(0) * primes[i &amp; 8].charCodeAt(0);
  }
  return key % 64;
}

答案3

得分: -1

Node.js 自版本 10.4 开始支持 BigInt。

你可以从文档中了解更多信息:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

或者查看这篇与 Node.js 更相关的概述:

http://thecodebarbarian.com/an-overview-of-bigint-in-node-js.html

英文:

Node.js supports BigInts since version 10.4

You can learn more from the docs:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

Or this overview here, which is more relevant to node:

http://thecodebarbarian.com/an-overview-of-bigint-in-node-js.html

huangapple
  • 本文由 发表于 2020年10月6日 12:23:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64219321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定