0

我知道这个问题的措辞很奇怪,所以让我用两个例子来解释一下。

示例 1
下面的代码将一系列值[64, 176, 0, 86, 68, 97, 136, 8]转换为 float 4096.336980910979

(new DataView(new Uint8Array([64, 176, 0, 86, 68, 97, 136, 8]).buffer)).getFloat64();
/*Output 4096.336980910979*/

输入 float 时如何反转它以获取一系列值?[64, 176, 0, 86, 68, 97, 136, 8]4096.336980910979

示例 2:
下面的代码将一系列值[70, 253, 192, 0]转换为 float 32480

(new DataView(new Uint8Array([70, 253, 192, 0]).buffer)).getFloat32();
/*Output 32480*/

输入 float 时如何反转它以获取一系列值?[70, 253, 192, 0]32480

4

1 回答 1

0

我已经创建了一个解决方案!

浮点32解决方案:

var arrayBuffer = new ArrayBuffer(4); /*Create an Array Buffer (Blank)*/
var floatArray = new Float32Array(arrayBuffer); /*Use the Float32 instance of the Buffer*/
var uintArray = new Uint8Array(arrayBuffer); /*Use the Uint8 instance of the Buffer*/

floatArray[0] = 32480; /*Define a value to the Float32Array*/
/*In order to correctly access the Uint8Array, it must be reversed for some reason?!*/
var newUintArray = uintArray.reverse();
var newarr = (new DataView(newUintArray.buffer)).getFloat32();

该变量newarr应等于 32480,并且该变量newUintArray可用于访问反向数据视图

浮点 64 解决方案:

var arrayBuffer = new ArrayBuffer(8);
var floatArray = new Float64Array(arrayBuffer);
var uintArray = new Uint8Array(arrayBuffer);

floatArray[0] = 30543;
var newUintArray = uintArray.reverse();
var newarr = (new DataView(newUintArray.buffer)).getFloat64();
于 2021-01-25T01:24:27.610 回答