javascript - Can I make refference to ArrayBuffer at offset? -
i convert data javascript variables arraybuffer
, back. think of function:
function numbertoarraybuffer(number) { return new float64array([number]).buffer; }
and other way around:
function numberfromarraybuffer(buffer) { // assuming implicitly, buffer long enough return new float64array(buffer, 0, 1)[0]; }
that works, imagine stuff more of these numbers in blob:
var numbers = new blob([numbertoarraybuffer(66), numbertoarraybuffer(666), numbertoarraybuffer(nan)], {type:"binary/float64array"});
if read using file reader, can arraybuffer
again:
var fl = new filereader(); fl.onload = function() { console.log(float64array(this.result)); } fl.readasarraybuffer(numbers);
and this:
float64array { 0=66, 1=666, 2=nan}
but of course, there sequences od different types, 2 unrestricted double
s , 4 uint32_t
. (but there no .offset
metod):
var num1 = numberfromarraybuffer(buffer); // shift 8 bytes - if `offset(8)` defined var num2 = bytefromarraybuffer(buffer.offset(8));
of course, can make "overloaded function":
function numbertoarraybuffer(number, offset) { return new float64array([number], offset||0).buffer; }
and use this:
var num2 = bytefromarraybuffer(buffer, 8);
but i'd prefer equivalent of c++ code:
void main() { uint8_t* bytes = loadbytesfromsomewhere(); // read 4 bytes , make int uint32_t integer = intfrombytes(bytes); // read 2 bytes , make short uint16_t short_integer = shortfrombytes(bytes+4); delete bytes; }
i want because it's annoying put offset aspect decoding algorithms. @ same time, want consistent , use arraybuffer
, can turned data type array @ will.
is there trick? tried this:
//make 20 bytes var buffer = new uint8array(20); console.log("original buffer: ",buffer.bytelength); //try offset buffer off cnt var shiftedarray = new uint8array(buffer, 10, 10); var shiftedbuffer = shiftedarray.buffer; console.log("shifted buffer? length:",shiftedbuffer.bytelength);
<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
not doesn't work, exhibits possible dangerous , confusing behavior of typed arrays created offset - length can smaller arraybuffer
length.
the following may or may not meet needs, believe it's equivalent of desired in c++ (minus delete bytes). though, old post , may have answer already!
as know, arraybuffers
in javascript can accessed through handful of different viewpoints/objects, called typedarrays. these objects such float64array, uint32array, etc. there typedarray object allows grab data dynamically whatever type @ target byte offsets called dataview
.
so achieve desired functionality following:
var buffer = loadbytebufferfromsomewhere(); //dataview same way var dv = new dataview(buffer); //uint32 @ index 0; dv.getuint32(0); //uint16 starting @ byte 4 dv.getuint16(4); //get target byte @ given indexes dv.getuint8(0); dv.getuint8(1);
dataview wrapper around arraybuffer float64array, uint32array, etc. can set target types @ given indexes well. i've found javascripture's references helpful typedarrays
Comments
Post a Comment