What does BE or LE mean in Buffer functions?

5.9k views Asked by At

I have a PHP class for reading Binary data that I'm converting to NodeJS or finding the equivalent of a couple functions in NodeJS. The functions I'm interested in this BinaryReader class are ReadULong and ReadUShort. I believe these mean read Unsigned Long integer (4 bytes) and Unsigned Short integer (2 bytes). As I'm trying to find the equivalent for these in NodeJS, I get confused on which function to use between these:

buf.readUInt16LE(offset, [noAssert])
buf.readUInt16BE(offset, [noAssert])

buf.readUInt32LE(offset, [noAssert])
buf.readUInt32BE(offset, [noAssert])

What would LE or BE stand for in this case?

The Buffer docs are located here but I was unable to find an explanation for those here.

Also I've found a constant on the PHP class that says const DEFAULT_BYTE_ORDER = 'L';. Is this L same as that L in readUInt32LE? Is this whole thing about Byte Orders?

So far I've read these articles:

If I could be given a couple more references to read about binary reading that would be much appreciated!

1

There are 1 answers

4
hexacyanide On BEST ANSWER

BE and LE stand for big endian and little endian. In big endian, the most significant byte is stored in the smallest address, and in little endian, the least significant byte is stored in the smallest address. That being said, endian does indicate the byte order. You can see the pattern in one of the examples in the documentation:

var buf = new Buffer(2);

buf[0] = 0x3;
buf[1] = 0x4;

buf.readUInt16BE(0);
buf.readUInt16LE(0);

// 0x0304
// 0x0403