Permalink
Browse files

working example

  • Loading branch information...
0 parents commit 31a60b00f3279895786e03b6210b61d301a6b4a0 @substack committed Nov 1, 2012
Showing with 26 additions and 0 deletions.
  1. +5 −0 example/bits.js
  2. +16 −0 index.js
  3. +5 −0 readme.markdown
@@ -0,0 +1,5 @@
+var toggle = require('../');
+var buf = new Buffer('abcd');
+
+console.dir(buf);
+console.dir(toggle(buf, 16));
@@ -0,0 +1,16 @@
+module.exports = function (buf, bits) {
+ var output = new Buffer(buf.length);
+
+ var bytes = bits / 8;
+ if (buf.length % bytes !== 0) {
+ throw new Error((buf.length % bytes) + ' non-aligned trailing bytes');
+ }
+
+ for (var i = 0; i < buf.length; i += bytes) {
+ for (var j = 0; j < bytes; j++) {
+ output[i + bytes - j - 1] = buf[i + j];
+ }
+ }
+
+ return output;
+};
@@ -0,0 +1,5 @@
+# endian-toggle
+
+Toggle the byte ordering of a buffer.
+
+

0 comments on commit 31a60b0

Please sign in to comment.