Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Started Buffer

  • Loading branch information...
commit 2f5efc643a301ce908c149b5814b6c45c484fa7c 1 parent c3bbdff
@tj tj authored
Showing with 23 additions and 1 deletion.
  1. +15 −1 chapters/buffers.md
  2. +8 −0 src/buffer/examples.js
View
16 chapters/buffers.md
@@ -1,4 +1,18 @@
# Buffers
- ...
+ To handle binary data, node provides us the `Buffer` object. Buffer instances represent memory allocated independently to that of V8's heap. There are several ways to constructor a `Buffer` instance, and many ways you can manipulate it's data.
+
+The simplest way to construct a `Buffer` from a string is to simply pass a string as the first argument. As you can see by the log output, we now have a buffer object containing 5 bytes of data represented in hexadecimal.
+
+ var hello = new Buffer('Hello');
+
+ console.log(hello);
+ // => <Buffer 48 65 6c 6c 6f>
+
+ console.log(hello.toString());
+ // => "Hello"
+
+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
8 src/buffer/examples.js
@@ -0,0 +1,8 @@
+
+var hello = new Buffer('Hello');
+console.log(hello);
+console.log(hello.toString());
+
+var hello = new Buffer([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
+console.log(hello);
+console.log(hello.toString());
Please sign in to comment.
Something went wrong with that request. Please try again.