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?
UPDATE
It should also work for an array like:
var array = [3,7426,78921]