Skip to content
uupaa edited this page Sep 25, 2015 · 21 revisions

TypedArray.js は、TypedArray に不足している機能を補うライブラリです。

Endian

動作環境におけるエンディアンの情報と、エンディアンの変換を行います。

TypedArray.BIG_ENDIAN                           // -> false (ARM, Intel CPU)
TypedArray.hton16( new Uint8Array([1,2]) )      // -> [2, 1]
TypedArray.ntoh16( new Uint8Array([1,2]) )      // -> [2, 1]
TypedArray.hton16( TypedArray.ntoh16( new Uint8Array([1,2]) ) ) // -> [1, 2]

Expand

バッファを2倍に拡張し、元の配列の内容を引き継いだ新しい配列を生成します

TypedArray.expand( new Uint32Array([1,2,3]) )   // -> Uint32Array([1,2,3,0,0,0])

文字列に変換

TypedArray.toString( new Uint8Array([0x33, 0x34, 0x35, 0x36]) ) // -> "3456"
TypedArray.fromString("Hello")                  // -> [72, 101, 108, 108, 111]
TypedArray.fromString("あいう")                 // -> [66, 68, 70]
TypedArray.fromString("あいう", Uint16Array)    // -> [12354, 12356, 12358]

ArrayBuffer に変換

URL/FILE/BLOB/BLOBURL/TypedArray/ArrayBuffer を ArrayBuffer に変換します

TypedArray.toArrayBuffer("http://example.com/404.png", function(arrayBuffer) {
    var buffer = new Uint8Array(arrayBuffer);
    ...
}, function(error) {
    ...
});

HexDump

16進ダンプです。デバッグのお供に

WebModule.TypedArray.hexDump( WebModule.TypedArray.fromString("あいうえお", Uint32Array) );

ADRESS        0        1        2        3
------ -------- -------- -------- --------
000000 00003042 00003044 00003046 00003048
000004 0000304a

WebModule.TypedArray.hexDump( WebModule.TypedArray.fromString("あいうえお", Uint16Array) );

ADRESS    0    1    2    3    4    5    6    7
------ ---- ---- ---- ---- ---- ---- ---- ----
000000 3042 3044 3046 3048 304a

WebModule.TypedArray.hexDump( WebModule.TypedArray.fromString("あいうえお", Uint8Array));

ADRESS  0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
------ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
000000 42 44 46 48 4a
Clone this wiki locally