Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Add reading/writing of C integers to buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
rmustacc authored and ry committed May 1, 2011
1 parent 8a03cf7 commit 9812e31
Show file tree
Hide file tree
Showing 6 changed files with 1,043 additions and 3 deletions.
195 changes: 195 additions & 0 deletions doc/api/buffers.markdown
Expand Up @@ -168,3 +168,198 @@ from the original Buffer.


// abc // abc
// !bc // !bc

### buffer.readUInt8(offset, endian)

Reads an unsigned 8 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.

Example:

var buf = new Buffer(4);

buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;

for (ii = 0; ii < buf.length; ii++) {
console.log(buf.readUInt8(ii, 'big');
console.log(buf.readUInt8(ii, 'little');
}

// 0x3
// 0x3
// 0x4
// 0x4
// 0x23
// 0x23
// 0x42
// 0x42

### buffer.readUInt16(offset, endian)

Reads an unsigned 16 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.

Example:

var buf = new Buffer(4);

buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;

console.log(buf.readUInt16(0, 'big');
console.log(buf.readUInt16(0, 'little');
console.log(buf.readUInt16(1, 'big');
console.log(buf.readUInt16(1, 'little');
console.log(buf.readUInt16(2, 'big');
console.log(buf.readUInt16(2, 'little');

// 0x0304
// 0x0403
// 0x0423
// 0x2304
// 0x2342
// 0x4223

### buffer.readUInt32(offset, endian)

Reads an unsigned 32 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.

Example:

var buf = new Buffer(4);

buf[0] = 0x3;
buf[1] = 0x4;
buf[2] = 0x23;
buf[3] = 0x42;

console.log(buf.readUInt32(0, 'big');
console.log(buf.readUInt32(0, 'little');

// 0x03042342
// 0x42230403

### buffer.readInt8(offset, endian)

Reads a signed 8 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.

Works as `buffer.readUInt8`, except buffer contents are treated as twos
complement signed values.

### buffer.readInt16(offset, endian)

Reads a signed 16 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.

Works as `buffer.readUInt16`, except buffer contents are treated as twos
complement signed values.

### buffer.readInt32(offset, endian)

Reads a signed 32 bit integer from the buffer at the specified offset. Endian
must be either 'big' or 'little' and specifies what endian ordering to read the
bytes from the buffer in.

Works as `buffer.readUInt32`, except buffer contents are treated as twos
complement signed values.

### buffer.writeUInt8(value, offset, endian)

Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 8 bit unsigned integer.

Example:

var buf = new Buffer(4);
buf.writeUInt8(0x3, 0, 'big');
buf.writeUInt8(0x4, 1, 'big');
buf.writeUInt8(0x23, 2, 'big');
buf.writeUInt8(0x42, 3, 'big');

console.log(buf);

buf.writeUInt8(0x3, 0, 'little');
buf.writeUInt8(0x4, 1, 'little');
buf.writeUInt8(0x23, 2, 'little');
buf.writeUInt8(0x42, 3, 'little');

console.log(buf);

// <Buffer 03 04 23 42>
// <Buffer 03 04 23 42>

### buffer.writeUInt16(value, offset, endian)

Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 16 bit unsigned integer.

Example:

var buf = new Buffer(4);
buf.writeUInt16(0xdead, 0, 'big');
buf.writeUInt16(0xbeef, 2, 'big');

console.log(buf);

buf.writeUInt16(0xdead, 0, 'little');
buf.writeUInt16(0xbeef, 2, 'little');

console.log(buf);

// <Buffer de ad be ef>
// <Buffer ad de ef be>

### buffer.writeUInt32(value, offset, endian)

Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 32 bit unsigned integer.

Example:

var buf = new Buffer(4);
buf.writeUInt32(0xfeedface, 0, 'big');

console.log(buf);

buf.writeUInt32(0xfeedface, 0, 'little');

console.log(buf);

// <Buffer fe ed fa ce>
// <Buffer ce fa ed fe>

### buffer.writeInt8(value, offset, endian)

Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 16 bit signed integer.

Works as `buffer.writeUInt8`, except value is written out as a two's complement
signed integer into `buffer`.

### buffer.writeInt16(value, offset, endian)

Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 16 bit unsigned integer.

Works as `buffer.writeUInt16`, except value is written out as a two's complement
signed integer into `buffer`.

### buffer.writeInt32(value, offset, endian)

Writes `value` to the buffer at the specified offset with specified endian
format. Note, `value` must be a valid 16 bit signed integer.

Works as `buffer.writeUInt832, except value is written out as a two's complement
signed integer into `buffer`.

0 comments on commit 9812e31

Please sign in to comment.