haxe.io.UInt8Array to haxe.io.Bytes #if js

I’m having some issues converting from haxe.io.UInt8Array to Bytes on the js target.
specifically a UInt8Array created with the subarray() method.

		var bytes:haxe.io.UInt8Array = new haxe.io.UInt8Array(10);
		var sub = bytes.subarray(2, 2);
		var test = sub.view.buffer;
		js.Browser.alert(test.length); //alerts 10 (I would expect 2)


Anybody know what’s going on?

Never used those but it seems you need to use sub.view.byteLength if you want to know the sub’s length,
and start from sub.view.byteOffset if you directly access the buffer.

Also the length is 0 not 2 since subarray takes a begin index and an end index, not length.

solved it by getting a sub array through the ArrayBufferView

var sub = bytes.view.sub(2,2);
var test = UInt8Array.fromBytes(sub.buffer);

this new UInt8Array has length 2, so this seems to be a copy :slight_smile: