Endianness defines the order of the sequence of multibyte data types (int, float, etc.) stored in the computer memory.
In little-endian, the last byte of the binary representation of the multibyte data type is stored first.
In big-endian, the first byte of the binary representation of the multibyte data type is stored first.
Buffer.writeInt16BE() writes a big-endian 16-bit integer value to a buffer at a specified offset.
Buffer.writeInt16BE() method is defined as follows:
The method takes up to two parameters:
value - this is the 16-bit signed integer to be written to the buffer. If the value is outside the range of a signed 16-bit integer, an error is thrown.
offset - the number of bytes to skip before starting the write. The default value of offset is 0.
offsetis an optional parameter
The offset must satisfy the condition: 0 <= offset <=
returns the number of bytes in the buffer
Buffer.writeInt16BE() method returns the sum of the
offset and the number of bytes of written.
The code below shows the use of the
Buffer.writeInt16BE() method. We allocate a 2-byte buffer and write the value 0x0205 at offset 0. We then display the buffer:
//Allocate 2 bytes buffer const buf = Buffer.allocUnsafe(2); //Write 0x0205 to buffer at offset 0 console.log(buf.writeInt16BE(0x0205, 0)); //Display buffer console.log(buf);
View all Courses