buf methods and properties

All method and property specifications use the buf object, where not specified. The buf buffer is assumed to be initialized, such as with the var buf = new Buffer(256) statement.

buf[index]

Get and set the octet at the index.

Syntax
buf[index]
Parameters
buf
The name of the variable for the buffer.
index
The value of index refers to individual bytes. Valid values are in the hex range 0x00 - 0xFF, which is the decimal range 0 - 255.
Example
Copy, 1 byte at a time, an ASCII string into a buffer.
var str = "GatewayScript";
var buf = new Buffer (str.length);

for (var i=0; i < str.length; i++){
   buf[i] = str.charCodeAt(i);
}
console.log(buf);

buf.buffer

References a buffer object.

Syntax
buf.buffer
Parameters
buf
The name of the variable for the buffer.
Example
The buf.buffer property references the underlying ArrayBuffer object based on which this buffer object is created.
const arrayBuffer = new ArrayBuffer(16);
const buf = Buffer.from(arrayBuffer);

console.log(buf.buffer === arrayBuffer);
// Prints: true

buf.compare()

Compares two buffers.

Syntax
buf.compare(targetBuffer[,targetStart[,targetEnd[,sourceStart[,sourceEnd]]]])
Parameters
buf
The name of the variable for the buffer.
targetBuffer
Buffer or Uint8Array object. The target buffer to compare to buf.
targetStart
Number. The offset within the target buffer at which to begin comparison. The default value is 0.
targetEnd
Number. The offset with the targetBuffer target buffer at which to end comparison (not inclusive). This parameter is ignored when targetStart is undefined. The default value is the length of the target buffer (target.length).
sourceStart
Number. The offset within the buf source buffer at which to begin comparison. This parameter is ignored when targetStart is undefined. The default value is 0.
sourceEnd
Number. The offset within the buf source buffer at which to end comparison (not inclusive). This parameter is ignored when targetStart is undefined. The default value is the buffer length (buffer.length).
Guidelines

Returns a number that indicates whether the buf source buffer is before, after, or the same as the targetBuffer target buffer in sort order. The comparison is the same as Buffer.compare(buf,targetBuffer).

0
Returned when buf is the same as targetBuffer.
1
Returned when buf is before targetBuffer when sorted.
-1
Returned when buf is after targetBuffer when sorted.
Example
const buf1 = Buffer.from('ABC');
const buf2 = Buffer.from('BCD');
const buf3 = Buffer.from('ABCD');

// Prints: 0
console.log(buf1.compare(buf1));

// Prints: -1
console.log(buf1.compare(buf2));

// Prints: -1
console.log(buf1.compare(buf3));

// Prints: 1
console.log(buf2.compare(buf1));

// Prints: 1
console.log(buf2.compare(buf3));

// Prints: [ <Buffer 41 42 43>, <Buffer 41 42 43 44>, <Buffer 42 43 44> ]
// This result is equal to: [buf1, buf3, buf2]
console.log([buf1, buf2, buf3].sort(Buffer.compare));

buf.copy()

Copy between source and target buffers.

Syntax
buf.copy([targetBuffer,] targetStart, sourceStart, sourceEnd)
Parameters
buf
The name of the variable for the buffer.
targetBuffer
Buffer object. The buffer to copy into.
targetStart
Number. Begin copying into the target at this position. The default value is 0.
sourceStart
Number. Begin copying from the source at this position. The default value is 0.
sourceEnd
Number. Stop copying from the source at this position. The default value is the buffer length (buffer.length).
Guidelines
The source and target regions can overlap. Any passed values that are undefined are set equal to their respective defaults. Undefined values are either not a number (NaN) or are out of bounds.
Example
Build two buffers. Copy byte 16 - 19 from buffer buf1 into buffer buf2 starting at the eighth byte in buffer buf2.
var buf1 = new Buffer(26);
var buf2 = new Buffer(26);

for (var i=0; i<26; i++) {
   buf1[i] = i + 97;         // 97 is ASCII a
   buf2[i] = 33;             // ASCII !
}

buf1.copy(buf2, 8, 16, 20);
console.log(buf2.toString('ascii', 0, 25);

buf.equals()

Compares two buffers and validates whether they are the same.

Syntax
buf.equals(buf1)
Parameters
buf
The name of the variable for the buffer.
buf1
The name of the target variable for the buffer.
Guidelines
Returns true if the target buffer buf1 contains the same bytes as the buf buffer; otherwise, false.

buf.fill()

Fills a buffer with the specified value that starts at the offset.

Syntax
buf.fill(value[,offset[,end]][,encoding])
Parameters
buf
The name of the variable for the buffer.
value
The value to use to fill to the buffer.
offset
Number. The offset to the beginning of the area in the buffer to fill. The default value is 0.
end
Number. The end position (not inclusive) of buffer to fill (buffer.length). If not specified, the entire buffer is filled.
encoding
The encoding of the value if value is a string. The default encoding is utf8.
Example
Fill a buffer with the letter h.
var b = new Buffer(50);
b.fill("h");

buf.includes()

Controls whether the value content is searched.

Syntax
buf.includes(value[,byteOffset][,encoding])
Parameters
buf
The name of the variable for the buffer.
value
String. A buffer object, or an integer that indicates what to search for.
byteOffset
Integer. Where to begin searching in the buf buffer object. The default value is 0.
encoding
String. An encoding. If the type of the value parameter is a string, this parameter is the encoding of that string. The default encoding is utf8.
Guidelines
The buf.includes() method controls whether the value content is searched. Returns true when the value content is searched. Otherwise, returns false. The buf.includes() method is equivalent to the buf.indexOf()!== -1 statement.
Example
const buf = Buffer.from('this is a buffer');

// Prints: true
console.log(buf.includes('this'));

// Prints: true
console.log(buf.includes('is'));

// Prints: true
console.log(buf.includes(Buffer.from('a buffer')));

// Prints: true
// (97 is the decimal ASCII value for 'a')
console.log(buf.includes(97));

// Prints: false
console.log(buf.includes(Buffer.from('a buffer example')));

// Prints: true
console.log(buf.includes(Buffer.from('a buffer example').slice(0, 8)));

// Prints: false
console.log(buf.includes('this', 4));
See also
buf.indexOf()

buf.indexOf()

Returns the index of the first occurrence of the searched content in the buf object.

Syntax
buf.indexOf(value[,byteOffset][,encoding])
Parameters
buf
The name of the variable for the buffer.
value
A string, a buffer object, a Uint8Array type array, or an integer that indicates what to search for.
byteOffset
Integer. Where to begin searching in the buf buffer object. The default value is 0.
encoding
String. An encoding. If the type of the value parameter is a string, this parameter is the encoding of that string. The default encoding is utf8.
Guidelines
The buf.indexOf() method returns one of the following. The searching is conducted from front to back in the buf object.
  • The index of the first occurrence of the searched value content in the buf object.
  • -1 when the buf object does not contain the value.
The following rules apply to parameters.
value
A string.
The value parameter is interpreted according to the encoding.
An empty string.
An empty buffer object.
  • When the byteOffset parameter value is less than the value of the buf.length property, the value of the byteOffset parameter is returned by the method.
  • When the byteOffset parameter value is at least the value of the buf.length property, the value of the buf.length property is returned by the method.
A buffer object.
Uint8Array array
The value parameter is used in its entirety.
An integer.
The value parameter value is interpreted as an unsigned 8-bit integer value in the range 0 - 255.
A number.
The value is coerced to a valid byte value, an integer in the range 0 - 255.
byteOffset
  • Coerced to a number when not a number.
  • Coerced to NaN or 0 when {}, [], null, or undefined. A coerced value indicates that the whole buffer is searched.
Example
const buf = Buffer.from('this is a buffer');

// Prints: 0
console.log(buf.indexOf('this'));

// Prints: 2
console.log(buf.indexOf('is'));

// Prints: 8
console.log(buf.indexOf(Buffer.from('a buffer')));

// Prints: 8
// (97 is the decimal ASCII value for 'a')
console.log(buf.indexOf(97));

// Prints: -1
console.log(buf.indexOf(Buffer.from('a buffer example')));

// Prints: 8
console.log(buf.indexOf(Buffer.from('a buffer example').slice(0, 8)));

const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');

// Prints: 4
console.log(utf16Buffer.indexOf('\u03a3', 0, 'ucs2'));

// Prints: 6
console.log(utf16Buffer.indexOf('\u03a3', -4, 'ucs2'));
const b = Buffer.from('abcdef');
// Passing a value that's a number, but not a valid byte
// Prints: 2, equivalent to searching for 99 or 'c'
console.log(b.indexOf(99.9));
console.log(b.indexOf(256+99));

// Passing a byteOffset that coerces to NaN or 0
// Prints: 1, searching the whole buffer
console.log(b.indexOf('b',undefined));
console.log(b.indexOf('b',{}));
console.log(b.indexOf('b',null));
console.log(b.indexOf('b',[]));
See also
buf.length

buf.lastIndexOf()

Returns the index of the last occurrence of the searched content in the buffer object.

Syntax
buf.lastIndexOf(value[,byteOffset][,encoding])
Parameters
buf
The name of the variable for the buffer.
value
A string, a buffer object, a Uint8Array type array, or an integer that indicates what to search for.
byteOffset
An integer indicates where to begin searching in the buf buffer object. The default value is (buf.length - 1).
encoding
String. An encoding. If the type of the value parameter is a string, this parameter is the encoding of that string. The default encoding is utf8.
Guidelines
The buf.lastIndexOf() method returns the index of the last occurrence of the searched value content in the buf object or returns -1 when the buf object does not contain the searched value. The searching is conducted from back to front in the buf object.
The following rules apply to parameters.
value
A string.
The value parameter is interpreted according to the encoding.
An empty string.
An empty buffer object.
  • When the byteOffset parameter value is less than the value of the buf.length property, the value of the byteOffset parameter is returned by the method.
  • When the byteOffset parameter value is at least the value of the buf.length property, the value of the buf.length property is returned by the method.
A buffer object.
Uint8Array array
The value parameter is used in its entirety.
An integer.
The value parameter value is interpreted as an unsigned 8-bit integer value in the range 0 - 255.
A number.
The value is coerced to a valid byte value, an integer in the range 0 - 255.
byteOffset
  • Coerced to a number when not a number.
  • Coerced to NaN or 0 when {}, [], null, or undefined. A coerced value indicates that the whole buffer is searched.
Example
const buf = Buffer.from('this buffer is a buffer');

// Prints: 0
console.log(buf.lastIndexOf('this'));

// Prints: 17
console.log(buf.lastIndexOf('buffer'));

// Prints: 17
console.log(buf.lastIndexOf(Buffer.from('buffer')));

// Prints: 15
// (97 is the decimal ASCII value for 'a')
console.log(buf.lastIndexOf(97));

// Prints: -1
console.log(buf.lastIndexOf(Buffer.from('yolo')));

// Prints: 5
console.log(buf.lastIndexOf('buffer', 5));

// Prints: -1
console.log(buf.lastIndexOf('buffer', 4));

const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');

// Prints: 6
console.log(utf16Buffer.lastIndexOf('\u03a3', undefined, 'ucs2'));

// Prints: 4
console.log(utf16Buffer.lastIndexOf('\u03a3', -5, 'ucs2'));
See also
buf.length

buf.length

Returns the size of the buffer in bytes.

Syntax
buf.length
Parameters
buf
The name of the variable for the buffer.
Guidelines
The size is not necessarily the size of the contents. The length refers to the amount of memory that is allocated for the buffer. The allocated memory does not change when the contents of the buffer change.

buf.readInt8()

Reads an 8-bit signed integer from the buffer at the specified offset.

Syntax
buf.readInt8(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Guidelines
This method works the same as the buffer.readUInt8() method, except that the buffer contents are treated as the twos complement.

buf.readInt16BE()

Reads a 16-bit signed integer from the buffer at the specified offset in the big endian format.

Syntax
buf.readInt16BE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Guidelines
This method works the same as the buffer.readUInt16BE() method, except that buffer contents are treated as the twos complement.

buf.readInt16LE()

Reads a 16-bit signed integer from the buffer at the specified offset in the little endian format.

Syntax
buf.readInt16LE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Guidelines
This method works the same as the buffer.readUInt16BE() method, except that buffer contents are treated as the twos complement.

buf.readInt32BE()

Reads a 16-bit signed integer from the buffer at the specified offset in the big endian format.

Syntax
buf.readInt32BE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Guidelines
This method works the same as the buffer.readUInt32BE() method, except that buffer contents are treated as the twos complement.

buf.readInt32LE()

Reads a 16-bit signed integer from the buffer at the specified offset in the little endian format.

Syntax
buf.readInt32LE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Guidelines
This method works the same as the buffer.readUInt32LE() method, except that buffer contents are treated as the twos complement.

buf.readUInt8()

Reads an 8-bit unsigned integer from the buffer at the specified offset.

Syntax
buf.readUInt8(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.

buf.readUInt16BE()

Reads a 16-bit unsigned integer from the buffer at the specified offset in the big endian format.

Syntax
buf.readUInt16BE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Read the buffer and write to the system log.
var buf = new Buffer(4);
buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;
console.log(buf,readUInt16BE(0));  // 0x0304
console.log(buf,readUInt16BE(1));  // 0x0423
console.log(buf,readUInt16BE(2));  // 0x2342

buf.readUInt16LE()

Reads a 16-bit unsigned integer from the buffer at the specified offset in the little endian format.

Syntax
buf.readUInt16LE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Read the buffer and write to the system log.
var buf = new Buffer(4);
buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;
console.log(buf,readUInt16LE(0));  // 0x0403
console.log(buf,readUInt16LE(1));  // 0x2304
console.log(buf,readUInt16LE(2));  // 0x4223

buf.readUInt32BE()

Reads a 32-bit unsigned integer from the buffer at the specified offset in the big endian format.

Syntax
buf.readUInt32BE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Read the buffer and write to the system log.
var buf = new Buffer(4);
buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;
console.log(buf,readUInt32BE(0));  // 0x03042342

buf.readUInt32LE()

Reads a 32-bit unsigned integer from the buffer at the specified offset in the little endian format.

Syntax
buf.readUInt32LE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Read the buffer and write to the system log.
var buf = new Buffer(4);
buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;
console.log(buf,readUInt32LE(0));  // 0x42230403

buf.readFloatBE()

Reads a 32-bit floating point number from the buffer at the specified offset in the big endian format.

Syntax
buf.readFloatBE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.

buf.readFloatLE()

Reads a 32-bit floating point number from the buffer at the specified offset in the little endian format.

Syntax
buf.readFloatLE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Read the buffer and write to the system log.
var buf= new Buffer(4);
buf[0] = 0x00;
buf[1] = 0x00;
buf[2] = 0x80;
buf[3] = 0x3f;
console.log(buf.readFloatLE(0));  // 0x01

buf.readDoubleBE()

Reads a 64-bit double number from the buffer at the specified offset in the big endian format.

Syntax
buf.readDoubleBE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.

buf.readDoubleLE()

Reads a 64-bit double number from the buffer at the specified offset in the little endian format.

Syntax
buf.readDoubleLE(offset)
Parameters
buf
The name of the variable for the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Read the buffer and write to the system log.
var buf= new Buffer(8);
buf[0] = 0x55;
buf[1] = 0x55;
buf[2] = 0x55;
buf[3] = 0x55;
buf[4] = 0x55;
buf[5] = 0x55;
buf[6] = 0x55;
buf[7] = 0x3f;
console.log(buf.readDoubleLE(0));  // 0.3333333333333333

buf.slice()

Returns a new buffer that references the same memory as the old buffer.

Syntax
buf.slice([start[,end]])
Parameters
buf
The name of the variable for the buffer.
start
Number. The starting index for the range. The default value is 0.
end
Number. The ending index for the range. The default value is the buffer length (buffer.length).
Guidelines
The new buffer can be cropped with a range set by the starting and ending indexes. Modifying the new buffer slice modifies the memory in the original buffer. Negative starting and ending indexes start from the end of the buffer.
Example
Build a buffer with the ASCII alphabet. Take a slice. Modify 1 byte from the original buffer.
var buf1 = new Buffer(26);

for (var i=0; i<26; i++) { // add letters to the buffer
  buf1[i] = i + 97;        // 97 is ASCII a
}

var buf2 = buf1.slice(0, 3);
console.log(buf2.toString('ascii', 0, buf2.length);  // abc
buf1[0] = 33;
console.log(buf2.toString('ascii', 0, buf2.length);  //!bc

buf.swap16()

Interprets the buffer object as an array of unsigned 16-bit integers and swaps the byte order in place.

Syntax
buf.swap16()
Parameters
buf
The name of the variable for the buffer.
Guidelines
The buffer size of the buf object must be a multiple of 16 bits. Otherwise, the buf.swap16() method raises an exception.
Example
const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);

// Prints: <Buffer 01 02 03 04 05 06 07 08>
console.log(buf1);

buf1.swap16();

// Prints: <Buffer 02 01 04 03 06 05 08 07>
console.log(buf1);

const buf2 = Buffer.from([0x1, 0x2, 0x3]);

// Throws an exception: RangeError: Buffer size must be a multiple of 16-bits
buf2.swap16();

buf.swap32()

Interprets the buffer object as an array of unsigned 32-bit integers and swaps the byte order in place.

Syntax
buf.swap32()
Parameters
buf
The name of the variable for the buffer.
Guidelines
The buffer size of the buf object must be a multiple of 32 bits. Otherwise, the buf.swap32() method raises an exception.
Example
const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);

// Prints: <Buffer 01 02 03 04 05 06 07 08>
console.log(buf1);

buf1.swap32();

// Prints: <Buffer 04 03 02 01 08 07 06 05>
console.log(buf1);

const buf2 = Buffer.from([0x1, 0x2, 0x3]);

// Throws an exception: RangeError: Buffer size must be a multiple of 32-bits
buf2.swap32();

buf.swap64()

Interprets the buffer object as an array of unsigned 64-bit integers and swaps the byte order in place.

Syntax
buf.swap64()
Parameters
buf
The name of the variable for the buffer.
Guidelines
The buffer size of the buf object must be a multiple of 64 bits. Otherwise, the buf.swap64() method raises an exception.
Example
const buf1 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);

// Prints: <Buffer 01 02 03 04 05 06 07 08>
console.log(buf1);

buf1.swap64();

// Prints: <Buffer 08 07 06 05 04 03 02 01>
console.log(buf1);

const buf2 = Buffer.from([0x1, 0x2, 0x3]);

// Throws an exception: RangeError: Buffer size must be a multiple of 64-bits
buf2.swap64();

buf.toJSON()

Returns a JSON representation of the buffer. A JSON.stringify function implicitly calls this function when a buffer is a JSON string.

Syntax
buf.toJSON()
Parameters
buf
The name of the variable for the buffer.
Example
var buf = new Buffer('test');
var json = JSON.stringify(buf);
console.log(json);

var copy = JSON.parse(json, function(key, value) {
  return value && value.type === 'Buffer'
    ? new Buffer(value.data)
    : value;
});
console.log(copy);

// '{"type":"Buffer","data":[116,101,115,116]}'
// <Buffer 74 65 73 74>

buf.toString()

Decodes and returns a string from buffer data with an encoding from and defined start and end position.

Syntax
buf.toString([encoding][,start][,end])
Parameters
buf
The name of the variable for the buffer.
encoding
The encoding of the source string. The default value is utf8.
start
Number. The position that begins the source string. The default value is 0.
end
Number. The position that ends the source string. The default value is the length of the buffer (buffer.length).
Example
Write to the buffer and system log.
var buf = new Buffer(256);
var len = buf.write('\u00bd + \u00bc = 'u00be' , 0);
console.log(len + " bytes: " + buf.toString('utf8', 0, len));

buf.write()

Writes a string to a buffer at the offset for the number of bytes with the specified encoding.

Syntax
buf.write(string[,offset[,length]][,encoding])
Parameters
buf
The name of the variable for the buffer.
string
String The data string to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
length
Number. The number of bytes to write. The default value is buffer.length - offset. If buffer does not contain sufficient space for the entire string, a partial string is written.
encoding
Encoding. The default value is utf8.
Guidelines
Returns the number of octets written. The method does not write partial characters.
Example
Write to the buffer and system log.
var buf = new Buffer(256);
var len = buf.write('\u00bd + \u00bc = 'u00be' , 0);
console.log(len + " bytes: " + buf.toString('utf8', 0, len));

buf.writeDoubleBE()

Writes a value to the buffer as a 64-bit double that starts at the specified offset with the big endian format. If the value is not a 64-bit double, the behavior is not specified.

Syntax
buf.writeDoubleBE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Write to the buffer and system log.
var buf = new Buffer(8);
buf.writeDoubleBE(0xcadbeefcafebabe, 0);
console.log(buf);  // <Buffer 43 eb d5 b7 dd f9 5f d7>

buf.writeDoubleLE()

Writes a value to the buffer as a 64-bit double that starts at the specified offset with the little endian format. If the value is not a 64-bit double, the behavior is not specified.

Syntax
buf.writeDoubleLE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Write to the buffer and system log.
var buf = new Buffer(8);
buf.writeDoubleLE(0xcadbeefcafebabe, 0);
console.log(buf);  // <Buffer d7 5f f9 dd b7 d5 eb 43>

buf.writeFloatBE()

Writes a value to the buffer as a 32-bit floating point number that starts at the specified offset with the big endian format. If the value is not a 64-bit double, the behavior is not specified.

Syntax
buf.writeFloatBE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Write to the buffer and system log.
var buf = new Buffer(4);
buf.writeFloatBE(0xcafebabe, 0);
console.log(buf);  // <Buffer 4f 4a fe bb>

buf.writeFloatLE()

Writes a value to the buffer as a 32-bit floating point number that starts at the specified offset with the little endian format. If the value is not a 64-bit double, the behavior is not specified.

Syntax
buf.writeFloatLE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Write to the buffer and system log.
var buf = new Buffer(4);
buf.writeFloatLE(0xcafebabe, 0);
console.log(buf);  // <Buffer bb fe 4a 4f>

buf.writeInt8()

Writes a value to the buffer as a signed 8-bit integer that starts at the specified offset.

Syntax
buf.writeInt8(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Guidelines
This method works the same as the writeUInt8() method, except that buffer contents are treated as the twos complement.

buf.writeInt16BE()

Writes a value to the buffer as a signed 16-bit integer that starts at the specified offset with the big endian format.

Syntax
buf.writeInt16BE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.

buf.writeInt16LE()

Writes a value to the buffer as a signed 16-bit integer that starts at the specified offset with the little endian format.

Syntax
buf.writeInt16LE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.

buf.writeInt32BE()

Writes a value to the buffer as a signed 32-bit integer that starts at the specified offset with the big endian format.

Syntax
buf.writeInt32BE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.

buf.writeInt32LE()

Writes a value to the buffer as a signed 32-bit integer that starts at the specified offset with the little endian format.

Syntax
buf.writeInt32LE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.

buf.writeUInt8()

Writes a value to the buffer as an unsigned 8-bit integer that starts at the specified offset.

Syntax
buf.writeUInt8(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Write to the buffer and system log.
var buf = new Buffer(4);
buf.writeUInt8(0x3, 0);
buf.writeUInt8(0x4, 1);
buf.writeUInt8(0x23, 2);
buf.writeUInt8(0x42, 3);
console.log(buf);  // <Buffer 03 04 23 42>

buf.writeUInt16BE()

Writes a value to the buffer as an unsigned 16-bit integer that starts at the specified offset with the big endian format.

Syntax
buf.writeUInt16BE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Write to the buffer and system log.
var buf = new Buffer(4);
buf.writeUInt16BE(0xdead, 0);
buf.writeUInt16BE(0xbeef, 2);
console.log(buf);  //<Buffer de ad be ef>

buf.writeUInt16LE()

Writes a value to the buffer as an unsigned 16-bit integer that starts at the specified offset with the little endian format.

Syntax
buf.writeUInt16LE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Write to the buffer and system log.
var buf = new Buffer(4);
buf.writeUInt16LE(0xdead, 0);
buf.writeUInt16LE(0xbeef, 2);
console.log(buf);  //<Buffer ad de ef be>

buf.writeUInt32BE()

Writes a value to the buffer as an unsigned 32-bit integer that starts at the specified offset with the big endian format.

Syntax
buf.writeUInt32BE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Write to the buffer and system log.
var buf = new Buffer(4);
buf.writeUInt32BE(0xfeedface, 0);
console.log(buf);  // <Buffer fe ed fa ce>

buf.writeUInt32LE()

Writes a value to the buffer as an unsigned 32-bit integer that starts at the specified offset with the little endian format.

Syntax
buf.writeUInt32LE(value,offset)
Parameters
buf
The name of the variable for the buffer.
value
Number. The value to write to the buffer.
offset
Number. The offset to the beginning of the buffer. The default value is 0.
Example
Write to the buffer and system log.
var buf = new Buffer(4);
buf.writeUInt32LE(0xfeedface, 0);
console.log(buf);  // <Buffer ce fa ed fe>