简体   繁体   English

将 8 字节 (u64) 无符号整数转换为 javascript 的建议

[英]Advice on converting 8 byte (u64) unsigned integer into javascript

I have a u64 (unsigned integer) stored in 8 bytes of memory.我有一个 u64(无符号整数)存储在 8 个字节的内存中。 Clearly the range is 0-2^64 integers.显然,范围是 0-2^64 个整数。

I am converting it to a javascript number by turning each byte into hex and making a hex string:我通过将每个字节转换为十六进制并制作一个十六进制字符串将其转换为 javascript 数字:

let s = '0x'
s += buffer.slice(0,1).toString("hex")
s += buffer.slice(1,2).toString("hex")
...
n = parseInt(s)

Works great for everything I have done so far.非常适合我迄今为止所做的一切。

But when I look at how javascript stores numbers, I become unsure.但是当我查看 javascript 如何存储数字时,我变得不确定。 Javascript uses 8 bytes for numbers, but treats all numbers the same. Javascript 使用 8 个字节来表示数字,但对所有数字都一视同仁。 This internal javascript "number" representation can also hold floating point numbers.这个内部javascript“数字”表示也可以保存浮点数。

Can a javascript number store all integers from 0 to 2^64? javascript 数字可以存储从 0 到 2^64 的所有整数吗? seems not.似乎没有。

At what point do I get into trouble?我什么时候会遇到麻烦? What do people do to get round this?人们怎么做才能解决这个问题?

An unsigned 64 bit integer has the range of a 0 to 18.446.744.073.709.551.615.无符号 64 位整数的范围是 0 到 18.446.744.073.709.551.615。

You could use the Number wrapper object with the .MAX_VALUE property, it represents the maximum numeric value representable in JavaScript .您可以使用带有.MAX_VALUE属性的Number包装器对象,它表示 JavaScript 中可表示的最大数值

The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value, like double in Java or C#. JavaScript Number类型是双精度 64 位二进制格式 IEEE 754 值,就像 Java 或 C# 中的 double。

General Info:基本信息:

Integers in JS: JS中的整数:

JavaScript has only floating-point numbers. JavaScript 只有浮点数。 Integers appear internally in two ways.整数以两种方式在内部出现。 First, most JavaScript engines store a small enough number without a decimal fraction as an integer (with, for example, 31 bits) and maintain that representation as long as possible.首先,大多数 JavaScript 引擎将一个足够小的没有小数的数字存储为整数(例如,31 位)并尽可能长时间地保持该表示。 They have to switch back to a floating point representation if a number's magnitude grows too large or if a decimal fraction appears.如果数字的大小变得太大或出现小数,他们必须切换回浮点表示。 Second, the ECMAScript specification has integer operators: namely, all of the bitwise operators.其次,ECMAScript 规范具有整数运算符:即所有按位运算符。 Those operators convert their operands to 32-bit integers and return 32-bit integers.这些运算符将其操作数转换为 32 位整数并返回 32 位整数。 For the specification, integer only means that the numbers don't have a decimal fraction, and 32-bit means that they are within a certain range.对于规范,整数仅表示数字没有小数,32 位表示它们在一定范围内。 For engines, 32-bit integer means that an actual integer (non-floating-point) representation can usually be introduced or maintained.对于引擎,32 位整数意味着通常可以引入或维护实际的整数(非浮点)表示。

Ranges of integers整数范围

Internally, the following ranges of integers are important in JavaScript:在内部,以下整数范围在 JavaScript 中很重要:

  • Safe integers [1], the largest practically usable range of integers that JavaScript supports: 53 bits plus a sign, range (−2^53, 2^53) which relates to (+/-) 9.007.199.254.740.992安全整数 [1], JavaScript 支持的最大实际可用整数范围:53 位加一个符号,范围 (-2^53, 2^53) 与 (+/-) 9.007.199.254.740.992 有关
  • Array indices [2]: 32 bits, unsigned Maximum length: 2^32−1 Range of indices: [0, 2^32−1) (excluding the maximum length!)数组索引 [2]:32 位,无符号 最大长度:2^32-1 索引范围:[0, 2^32-1)(不包括最大长度!)
  • Bitwise operands [3]: Unsigned right shift operator (>>>): 32 bits, unsigned, range [0, 2^32) All other bitwise operators: 32 bits, including a sign, range [−2^31, 2^31)按位操作数 [3]:无符号右移运算符 (>>>):32 位,无符号,范围 [0, 2^32) 所有其他按位运算符:32 位,包括符号,范围 [−2^31, 2^ 31)
  • “Char codes”, UTF-16 code units as numbers: Accepted by String.fromCharCode() Returned by String.prototype.charCodeAt() 16 bit, unsigned “字符代码”,UTF-16 编码单元为数字: String.fromCharCode() 接受 返回 String.prototype.charCodeAt() 16 位,无符号

References:参考:

[1] Safe integers in JavaScript [1] JavaScript 中的安全整数

[2] Arrays in JavaScript [2] JavaScript 中的数组

[3] Label bitwise_ops [3] 标签 bitwise_ops

Source: https://2ality.com/2014/02/javascript-integers.html来源: https : //2ality.com/2014/02/javascript-integers.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM