buf.writeIntBE() function writes a given value at a given offset in the buffer that calls the
buf.writeIntBE() function accepts the following three parameters:
value: The value that is written to the buffer. Its maximum size is 48 bits or 6 bytes.
offset: The position in the buffer where the value is written. An
offsetof 3 would mean the value is written at the fourth-byte position.
offsetmust fulfill the condition
0 <= offset <= buf.length - byteLength.
byteLength: The number of bytes that are written to the buffer.
byteLengthmust be greater than zero and less than or equal to 6.
buf.writeIntLE() function returns the number of bytes written plus the offset as an integer value.
value is written to the buffer in the big-endian format.
In the big-endian format, the most significant byte is stored first and the least significant byte is stored last. In the little-endian format, the least significant byte is stored first and the most significant byte is stored last. For example, a hexadecimal value A1B2C3 will be stored as C3B2A1 in the little-endian format and as A1B2C3 in the big-endian format.
The following example demonstrates how to write a 48-bit value to a buffer.
//create a buffer var buffer = Buffer.alloc(10) console.log(buffer) //define value value = 0x192837465111 //write to buffer buffer.writeIntBE(value, 2, 6) console.log(buffer)
The program creates a buffer of zeros of size 10 bytes. It then defines the value and writes it to the buffer, starting from the third byte. The values in the buffer are in the big-endian format as illustrated.
The program will display an undefined behavior if the value is not a signed integer.
View all Courses