0

I created this function to parse a Integer from a Buffer:

var makeInt = function(b1, b2, b3, b4) {
    return ((b1 <<  0) & 0x000000FF) +   
           ((b2 <<  8) & 0x0000FF00) + 
           ((b3 << 16) & 0x00FF0000) + 
           ((b4 << 24) & 0xFF000000);
} 

From a buffer I read the Integer like that:

var buffer = new Buffer([0,0,15,47,0,0,0,64,0,0])

console.log(makeInt(buffer[3],buffer[2],buffer[1],buffer[0]))

=> 3887

What's the official Buffer function from https://nodejs.org/api/buffer.html that does the same like my makeInt function?

I tried https://nodejs.org/api/buffer.html#buffer_buf_readuintbe_offset_bytelength_noassert

But buf.readUIntLE(offset, byteLength[, noAssert]) returns:

buffer.readUIntLE(0, 3)

=> 983040

Why is it not returning 3887 != 983040?

Thanks

1 Answer 1

2

You use 4 bites, but passed 3. There are two ways to store number - little-endian and big-endian. It seems your code realized big-endian.

var buffer = new Buffer([0,0,15,47,0,0,0,64,0,0]);
console.log(buffer.readUIntBE(0, 4));
Sign up to request clarification or add additional context in comments.

1 Comment

Works, Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.