从数组创建ArrayBuffer(保留整数),然后再次返回 [英] Create ArrayBuffer from Array (holding integers) and back again
问题描述
这似乎很简单,但是我无法找出如何将填充有整数的Array
转换为ArrayBuffer
并再次转换回Array
.有很多将字符串转换为ArrayBuffer
的示例,例如
It seems so simple, but I cannot find out how to convert an Array
filled with integers to an ArrayBuffer
and back again to an Array
. There are lots of examples where strings are converted to an ArrayBuffer
like for example here.
Using these examples I created this:
/**
* Convert string to array buffer.
*
* @param {Array.<int>} array
* @returns {ArrayBuffer}
*/
self.arrayToArrayBuffer = function( array ) {
var length = array.length;
var buffer = new ArrayBuffer( length * 2 );
var view = new Uint16Array(buffer);
for ( var i = 0; i < length; i++) {
view[i] = array[i];
}
return buffer;
}
然后,该数组也需要再次转换回原先.为此,我使用:
Then the array also needs to converted back again. For this I use:
var array = new Uint16Array(arrayBuffer);
此解决方案似乎有效,但是没有更简单的方法吗?
This solution seems to work, but is there no easier way to do this?
它也应适用于以下数组:
It should also work for an array like:
var array = [3,7426,78921]
推荐答案
是的,有一种无需手动编写循环的简单方法(循环仍存在于后台某处):
Yes, there's a simple way without manually writing a loop (the loop still exists somewhere in background):
new Uint16Array([1,2,3]);
仅此而已.当然,浮点数将被四舍五入,大数将溢出.
That's all. Of course, floating numbers will be rounded down and big numbers will overflow.
任何类型的数组的缓冲区都可以通过.buffer
属性访问,如 任何人都可以在MDN上阅读:
The buffer of any typed array is accessible through .buffer
property, as anyone can read on MDN:
new Uint16Array([1,2,3]).buffer;
选择正确的类型数组
请注意,提及的Uint16Array
仅将保留介于0和65535之间的整数(无浮点数).要保留任何javascript Number 1 ,您将要使用Float64Array
-最大的一个,总共占用8个字节.
Chosing the right typed array
Be warned that mentioned Uint16Array
will only hold integers (no floating point) between zero and 65535. To hold any javascript Number1 you will want to use Float64Array
- the bigest one, taking 8 bytes total.
1::这是不受限制的double,似乎是64位 IEEE 754编号
1: Which is unrestricted double, which appears to be 64bit IEEE 754 number
这是我创建的地图,其中映射了一些与数字数据类型有关的重要信息:
Here's a map I have created that maps some of the important information related to number data types:
var NUMBER_TYPE = [
{name: "uint8", bytes:1, max: 255, min: 0, floating: false, array: Uint8Array},
{name: "int8", bytes:1, max: 127, min: -128, floating: false, array: Int8Array},
{name: "uint16", bytes:2, max: 65535, min: 0, floating: false, array: Uint16Array},
{name: "int16", bytes:2, max: 32767, min: -32768, floating: false, array: Int16Array},
{name: "uint32", bytes:4, max: 4294967295, min: 0, floating: false, array: Uint32Array},
{name: "int32", bytes:4, max: 2147483647, min: -2147483648, floating: false, array: Int32Array},
{name: "float64", bytes:8, max: Number.MAX_VALUE, min: Number.MIN_VALUE, floating: true , array: Float64Array}
];
缺少第32浮点数,因为我无法为其计算必要的信息.地图本身可以用来计算您可以将Number放入其中的最小类型的数组:
Float 32 is missing as I was unable to calculate necessary information for it. The map, as it is, can be used to calculate the smallest typed array you can fit a Number in:
function findNumberType(num) {
// detect whether number has something after the floating point
var float = num!==(num|0);
// Prepare the return variable
var type = null;
for(var i=0,l=NUMBER_TYPE.length; i<l; i++) {
// Assume this type by default - unless break is hit, every type ends as `float64`
type = NUMBER_TYPE[i];
// Comparison asserts that number is in bounds and disalows floats to be stored
// as integers
if( (!float || type.floating) && num<=type.max && num>=type.min) {
// If this breaks, the smallest data type has been chosen
break;
}
}
return type;
}
用作:
var n = 1222;
var buffer = new (findNumberType(n).array)([n]);
请注意,这仅在正确订购NUMBER_TYPE
时有效.
Note that this only works if NUMBER_TYPE
is properly ordered.
这篇关于从数组创建ArrayBuffer(保留整数),然后再次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!