Skip to content

Commit

Permalink
More buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 15, 2010
1 parent 997d771 commit 054e479
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions chapters/buffers.md
Expand Up @@ -13,6 +13,16 @@ The simplest way to construct a `Buffer` from a string is to simply pass a strin
console.log(hello.toString());
// => "Hello"

By default the encoding is "utf8", however this can be specified by passing as string as the second argument. The ellipsis below for example will be printed to stdout as the '&' character when in "ascii" encoding.

var buf = new Buffer('…');
console.log(buf.toString());
// => …

var buf = new Buffer('…', 'ascii');
console.log(buf.toString());
// => &

An alternative method is to pass an array of integers representing the octet stream, however in this case functionality equivalent.

var hello = new Buffer([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
Expand Down
4 changes: 3 additions & 1 deletion src/buffer/examples.js
Expand Up @@ -21,4 +21,6 @@ var buf = new Buffer(5);
buf.write('he');
buf.write('l', 2);
buf.write('lo', 3);
console.log(buf.toString());
console.log(buf.toString());

console.log(new Buffer('…', 'ascii').toString());

0 comments on commit 054e479

Please sign in to comment.