简体   繁体   English

如何获取javascript 64bit浮点数的实际字节数

[英]How to get the actual bytes of a javascript 64bit float

I have been trying to work out how to use typed array's in JavaScript but something just isn't clicking yet for me. 我一直在尝试研究如何在JavaScript中使用类型数组,但是我还没有点击。 I am not understand how to read/write to the "view" properly. 我不明白如何正确地读/写“视图”。

var a = 23453.342;

now let's say I want to store this in a Float64Array typed array 现在让我们说我想将它存储在一个Float64Array类型的数组中

view[0] = ??? //how do I copy the bytes that represent variable a into here.

Also let's say I want to store the integer 123456 that is in JavaScript internally as a 64Bit float: 另外,假设我想将内部JavaScript中的整数123456存储为64位浮点数:

var i = 123456;

but I would like to store it as if it was really a 4 byte integer. 但我想把它存储好像它真的是一个4字节的整数。

var buff = new ArrayBuffer(4);
var view = new UInt32Array(buff);

view[0] = ???; //how do i determine and write the 4 bytes that would represent the value of i.

Then also in both cases once I am able to write the data there how do I read it back into local variables in JavaScript. 然后在这两种情况下,一旦我能够写入数据,我如何在JavaScript中将其读回到局部变量中。

If your value is smaller than the Typed Array unit than just store the value into the array, eg you have 23453.342 and using Float64, then just put it into the array 如果你的值小于Typed Array单元而不是将值存储到数组中,例如你有23453.342并使用Float64,那么只需将它放入数组中

var view = new Float64Array(1);
view[0] = 23453.342;

Now if you are wanting to convert a number into its individual bytes,eg a 32bit number and get the 4 bytes that make it, you can use two TypedArray views. 现在,如果您想将数字转换为单个字节,例如32位数字并获得4个字节,则可以使用两个TypedArray视图。 Put your value into the higher bit array, and read the bytes from the lower unit array. 将您的值放入较高位数组,并从较低位单元读取字节。

 //The argument passed to ArrayBuffer constructor //is number of Bytes the buffer should be var buff = new ArrayBuffer(4); //Since buffer was made for 4 bytes, this view can read/store 1 32bit value var view = new Uint32Array(buff); //buffer still has 4 bytes, so this view can read/store 4 8bit values var view2 = new Uint8Array(buff); view[0] = 123456; console.log(view2); 

If you want to manually get the bytes, or do not have access to a higher unit typed array, than you can use the bit shifting and bitwise AND operators to get the individual bytes 如果要手动获取字节,或者无法访问更高单元类型的数组,则可以使用位移和按位AND运算符来获取单个字节

 var i = 123456; var buff = new ArrayBuffer(4); var view = new Uint32Array(buff); var view2 = new Uint8Array(buff); view2[0] = i & 0x000000FF; view2[1] = (i & 0x0000FF00) >> 8; view2[2] = (i & 0x00FF0000) >> 16; view2[3] = (i & 0xFF000000) >> 24; console.log( view2, view[0] ); 

As for getting the values back out, in the case of using the two typed arrays, you just read the array of the target unit. 至于将值取回,在使用两个类型数组的情况下,您只需读取目标单元的数组。 In this case read from the Uint32Array 在这种情况下,从Uint32Array中读取

 var buff = new ArrayBuffer(4); var view = new Uint32Array(buff); var view2 = new Uint8Array(buff); view2[0] = 64, view2[1] = 226, view2[2] = 1, view2[3] = 0; var i = view[0]; console.log(i); 

Again if you want to use the bit shifting way, you need to shift each byte from the smaller typed array and bitwise OR them together. 再次,如果你想使用位移方式,你需要从较小的类型数组中移位每个字节,并将它们按位或移位在一起。

 var buff = new ArrayBuffer(4); var view = new Uint8Array(buff); view[0] = 64, view[1] = 226, view[2] = 1, view[3] = 0; var i = 0; i |= view[3] << 24; i |= view[2] << 16; i |= view[1] << 8; i |= view[0]; console.log(i); 

When you create a TypedArray of a certain type, say Int8Array like this: 当你创建某种类型的TypedArray时,比如说Int8Array

let my = new Int8Array(1);

and assign a value to it, like this: 并为其赋值,如下所示:

my[0] = 123456;

a conversion based on the this table is performed. 执行基于此表的转换。 So, for Int8Array the method ToInt8 is used with the last steps being: 因此,对于Int8Array ,方法ToInt8用于最后的步骤:

... ...
Let int8bit be int modulo 2^8. 设int8bit为int modulo 2 ^ 8。
If int8bit ≥ 2^7, return int8bit − 2^8, otherwise return int8bit. 如果int8bit≥2^ 7,则返回int8bit - 2 ^ 8,否则返回int8bit。

So, if we run these steps 所以,如果我们运行这些步骤

Let int8bit be 123456 modulo 2^8 // 64
64 < 2^7 -> return int8bit, which is 64

So 64 is what you should expect: 所以你应该期待64

console.log(my[0]); // 64

If you're interested in the actual bits order, check this answer for the algorithm or this article for the details of floating point . 如果您对实际位顺序感兴趣,请查看算法或本文的答案以获取浮点详细信息

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

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