156

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

5 Answers 5

251

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried to do Buffer.from(bufStr, 'utf-8'); but it says utf-8 is not a function. If I omit the second argument utf-8 then it throws TypeError: this is not a typed array.
@Aniket: Probably your Node version is older. Try just Buffer(bufStr) instead until you upgrade.
Actually, it should be 'utf8' and not 'utf-8'. Edited the answer.
iconv-lite is better, because it uses standard charset names from world of client js (like utf-8), instread of NodeJS redefinitions (like utf8).
Buffer.from(str) is enough, as 'utf8' is default. nodejs.org/api/…
|
19

You can use Buffer.from() to convert a string to buffer. More information on this can be found here

var buf = Buffer.from('some string', 'encoding');

for example

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

Comments

16

Note: Just reposting John Zwinck's comment as answer.

One issue might be that you are using a older version of Node (for the moment, I cannot upgrade, codebase struck with v4.3.1). Simple solution here is, using the deprecated way:

new Buffer(bufferStr)

Note #2: This is for people struck in older version, for whom Buffer.from does not work

Comments

4

This is working for me, you might change your code like this

var responseData=x.toString();

to

var responseData=x.toString("binary");

and finally

response.write(new Buffer(toTransmit, "binary"));

2 Comments

Amazing I was looking for this only!
Where does toTransmit come from? This is seemingly randomly in your answer without context.
1

I think npm package must have been updated. Because the following function

let buf = Buffer.from(contractCode, 'utf-8')

seems to be working very well for me. I even cross verified by doing

console.log(String(buf))

and indeed the previous string is being returned to me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.