Created
September 6, 2015 07:43
-
-
Save slugbyte/ff8d077ad4f4a0e18842 to your computer and use it in GitHub Desktop.
read node js buffer with correct endianness
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
'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