问题描述
我知道这个问题的措辞很奇怪,所以让我用两个例子来解释。
示例 1
下面的代码将值系列 [64,176,86,68,97,136,8]
转换为浮点数 4096.336980910979
。
(new DataView(new Uint8Array([64,8]).buffer)).getFloat64();
/*Output 4096.336980910979*/
当我输入浮点数 [64,8]
时,我如何反转它以获得一系列值 4096.336980910979
?
示例 2:
下面的代码将值系列 [70,253,192,0]
转换为浮点数 32480
。
(new DataView(new Uint8Array([70,0]).buffer)).getFloat32();
/*Output 32480*/
当我输入浮点数 [70,0]
时,我如何反转它以获得一系列值 32480
?
解决方法
我已经创建了一个解决方案!
Float 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
可用于访问反向数据视图。
Float 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();