javascript: Uint8array vs. Bytes

1,377 views
Skip to first unread message

Nathan Hüsken

unread,
Jul 17, 2015, 7:47:36 AM7/17/15
to haxe...@googlegroups.com
Hey,

In haxe I usally use haxe.io.Bytes for binary data. But javascript
libraries usually return Uint8array.

Is there a way to convert between these types without copying all the data?
Bytes are used in the std library (for example Base64) and I want to use
those functions ...

best,
Nathan

azrafe7

unread,
Jul 18, 2015, 7:14:37 AM7/18/15
to haxe...@googlegroups.com

A minimal example:

import haxe.io.Bytes;
import haxe.io.UInt8Array;
import haxe.io.UInt16Array;

class Test {
    static function main() {
        var data8 = UInt8Array.fromArray([for (i in 0...10) i]);
        var data16:UInt16Array = cast data8;
        var bytes:Bytes = data8.view.buffer;

        trace(data8[0]);
        trace(data16[0]);
        trace(bytes.get(0));

        bytes.set(0, 0xFF);

        trace(data8[0]);
        trace(data16[0]);
        trace(bytes.get(0));
    }
}

http://try.haxe.org/#e8061

Nathan Hüsken

unread,
Jul 18, 2015, 8:33:57 AM7/18/15
to haxe...@googlegroups.com
Hey,

Thanks for the example. What I am still struggling with is the difference/conversion between Uint8Array (js.html) and Bytes (or UInt8Array form haxe.io).

Now, I just tried to explicit cast these types (js.html.Uint8Array -> haxe.io.UInt8Array) like this: http://try.haxe.org/#B339b

import haxe.io.Bytes;
import js.html.Uint8Array;
import haxe.io.UInt8Array;


class Test {
    static function main() {
        var data8js = new Uint8Array([for (i in 0...10) i]);
        var data8 : UInt8Array = cast data8js;

        var bytes:Bytes = data8.view.buffer;
       
        trace(data8[0]);
        trace(bytes.get(0));
       
        bytes.set(0, 0xFF);
       
        trace(data8[0]);
        trace(bytes.get(0));
    }
}

And it seems to work! But can I expect this to continue to work in future Haxe versions?
Should there not be a function for casting this?

Thanks!
Nathan
--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

vroad

unread,
Jul 18, 2015, 10:33:03 AM7/18/15
to haxe...@googlegroups.com
You should use unsafe cast only when you know what you are doing... though those two seems interchangable, as haxe.io.UInt8Array is an abstract class.

On js BytesData is ArrayBuffer, so you can create Bytes from existing ArrayBuffer. There is no need to do unsafe cast in this case.

import haxe.io.Bytes;
import js.html.Uint8Array;

class Test {
    static function main() {
        var data8 = new Uint8Array([for (i in 0...10) i]);
        var bytes:Bytes = Bytes.ofData(data8.buffer);
        
        trace(data8[0]);
        trace(bytes.get(0));
        
        bytes.set(0, 0xFF);
        
        trace(data8[0]);
        trace(bytes.get(0));
    }
}

Nathan Hüsken

unread,
Jul 19, 2015, 8:48:37 AM7/19/15
to haxe...@googlegroups.com
Cool!
Thanks.
Reply all
Reply to author
Forward
0 new messages