Skip to content

Instantly share code, notes, and snippets.

@slugbyte
Created September 6, 2015 07:43
Show Gist options
  • Save slugbyte/ff8d077ad4f4a0e18842 to your computer and use it in GitHub Desktop.
Save slugbyte/ff8d077ad4f4a0e18842 to your computer and use it in GitHub Desktop.
read node js buffer with correct endianness
'use strict';
var endianness = require('os').endianness();
var read8 = Buffer.prototype.readUInt8;
var read16 = Buffer.prototype['readUInt16' + endianness];
var read32 = Buffer.prototype['readUInt32' + endianness];
var readNumber = function(buf, readFunc, offset){
buf.readFunc = readFunc;
var result = buf.readFunc(offset);
delete buf.readFunc;
return result;
};
var buf = new Buffer('\0a buf thing hello world');
console.log(readNumber(buf, read16, 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment