Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created August 24, 2014 18:33
Show Gist options
  • Save miguelmota/5b06ae5698877322d0ca to your computer and use it in GitHub Desktop.
Save miguelmota/5b06ae5698877322d0ca to your computer and use it in GitHub Desktop.
Node.js Buffer to ArrayBuffer
// @credit: http://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer
// From Buffer to ArrayBuffer:
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;
}
// From ArrayBuffer to Buffer:
function toBuffer(ab) {
var buffer = new Buffer(ab.byteLength);
var view = new Uint8Array(ab);
for (var i = 0; i < buffer.length; ++i) {
buffer[i] = view[i];
}
return buffer;
}
@panzi
Copy link

panzi commented Jan 29, 2021

Ah I see. 😄
Anyway, I guess this is still useful if someone else finds this gist via Google.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment