3

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!

Community
  • 1
  • 1
Logan
  • 10,649
  • 13
  • 41
  • 54

1 Answers1

8

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
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
  • If we were to think about base 10 numbers, in `4567` LE would look like `7|6|5|4` in memory and BE would look like `4|5|6|7` right? I wonder why they separated those like that... To get the first digit easier with LE I guess? For example if I wanted `7` out of there, my arrayLE[0] would be `7` and arrayBE[3] would also be `7`. This would help if I didn't know how long the thing was I guess, but then again in the start I would be using `16bit` or `32bit` reading functions. Therefore wouldn't I already know how far the first byte would be in the first place? – Logan Sep 14 '13 at 18:45
  • Ah! After looking at the example knowing what BE and LE stands for I can see the significance of them. It is about finding out what byte is next to the offset, with BE; and finding out what byte is before than that offset with LE. Can you confirm that please? – Logan Sep 14 '13 at 18:48
  • So BE and LE is just finding out how the data is ordered so you can read it property. Take your example of `4567` and convert it to hexadecimal. If we have a two length buffer, `var buf = new Buffer(2);` and we set the values like this: `buf[0] = 0x11; buf[1] = 0xD7;`, we have this buffer: ``. This is big endian, so if we read like so: `buf.readUInt16BE(0)`, we get `4567`. If we try to read in little endian, we get `55057`. If we reverse the order of the buffer, `buf[0] = 0xD7; buf[1] = 0x11;`, it is now small endian, and `buf.readUInt16BE(0)` returns `4567`. – hexacyanide Sep 14 '13 at 19:16
  • 1
    Back to your original comment, `4567` in hexadecimal `11|D7` in big endian and `D7|11` in little endian. If you read the numbers in the wrong endian then you get `55057`. – hexacyanide Sep 14 '13 at 19:18