Permalink
Browse files

More buffers

  • Loading branch information...
1 parent 997d771 commit 054e4797278fc6a20a61e69d0cb152e4f56fe2a5 @tj tj committed Aug 15, 2010
Showing with 13 additions and 1 deletion.
  1. +10 −0 chapters/buffers.md
  2. +3 −1 src/buffer/examples.js
View
@@ -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]);
View
@@ -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.