18
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

node.jsでBuffer経由でArrayBufferを使う

Posted at

ブラウザ向けに書いていたコードをどうしてもサーバサイドでも使いたいと思い調べた結果以下のコードで使えることがわかった。

var fs = require('fs');

function toArrayBuffer(buffer) {
    var ab = new ArrayBuffer(buffer.length);
    var view = new Uint8Array(ab);
    for (var i = 0; i < buffer.length; ++i) {
        view[i] = buffer[i];
    }
    return ab;
}

var buf = fs.readFileSync('./hoge.bin');
var arrayBuf = toArrayBuffer(buf);

これでDataStream.jsをちょこっと変更するだけでnode.jsでも手軽にバイナリをいじれるようになった。

node向けに変更したDataStream.js

18
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?