Created
August 24, 2014 18:33
-
-
Save miguelmota/5b06ae5698877322d0ca to your computer and use it in GitHub Desktop.
Node.js Buffer to ArrayBuffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah I see. 😄
Anyway, I guess this is still useful if someone else finds this gist via Google.