DEV Community

Cover image for How to loop through Buffers in Node.js?
MELVIN GEORGE
MELVIN GEORGE

Posted on Originally published at melvingeorge.me

How to loop through Buffers in Node.js?

Originally posted here!

To loop through a Buffer instance we can use the entries() method available in the buffer instance with the for...of looping statement.

Let's say we have a Buffer instance with String Hello World! like his,

// Buffer instance with string Hello World!
const buff = Buffer.from("Hello World!");
Enter fullscreen mode Exit fullscreen mode

Now to loop through the Buffer let's use the entries() method and the for...of looping statement like this,

// Buffer instance with string Hello World!
const buff = Buffer.from("Hello World!");

// loop through buffer instance
for (let pair of buff.entries()) {
  console.log(pair);
}
Enter fullscreen mode Exit fullscreen mode
  • The entries() method returns the data in [index, byte] format.

The output of the above code looks like this.

/*

[ 0, 72 ]
[ 1, 101 ]
[ 2, 108 ]
[ 3, 108 ]
[ 4, 111 ]
[ 5, 32 ]
[ 6, 87 ]
[ 7, 111 ]
[ 8, 114 ]
[ 9, 108 ]
[ 10, 100 ]
[ 11, 33 ]

*/
Enter fullscreen mode Exit fullscreen mode

But if you don't want this in the [index, byte] format but as a single string, you can use the String.fromCharCode() method to get the appropriate character inside the loop like this,

// Buffer instance with string Hello World!
const buff = Buffer.from("Hello World!");

// loop through buffer instance
for (let pair of buff.entries()) {
  // get the byte of character
  const charCode = pair[1];
  // use String.fromCharCode() to get the appropriate character
  // for the byte
  console.log(String.fromCharCode(charCode));
}
Enter fullscreen mode Exit fullscreen mode

The output of the above code looks like this,

/*

H
e
l
l
o

W
o
r
l
d
!

*/
Enter fullscreen mode Exit fullscreen mode

See this example live in repl.it.

Feel free to share if you found this useful 😃.


Top comments (0)