Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to buffer Node

Tags:

node.js

I am using a library which on call of a function returns the toString of a buffer.

The exact code is

return Buffer.concat(stdOut).toString('utf-8'); 

But I don't want string version of it.

I just want the buffer

So how to convert string back to buffer.

Something like if

var bufStr = Buffer.concat(stdOut).toString('utf-8'); //convert bufStr back to only Buffer.concat(stdOut). 

How to do this?

I tried doing

var buf = Buffer.from(bufStr, 'utf-8'); 

But it throws utf-8 is not a function. When I do

var buf = Buffer.from(bufStr); 

It throws TypeError : this is not a typed array.

Thanks

like image 641
Aniket Avatar asked May 25 '16 12:05

Aniket


1 Answers

You can do:

var buf = Buffer.from(bufStr, 'utf8'); 

But this is a bit silly, so another suggestion would be to copy the minimal amount of code out of the called function to allow yourself access to the original buffer. This might be quite easy or fairly difficult depending on the details of that library.

like image 140
John Zwinck Avatar answered Oct 10 '22 07:10

John Zwinck