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.
Answer from John Zwinck on Stack OverflowYou 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.
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');
node.js 6.0.0 buffer to string equivalent in python3
Python equivalent of Node.js Buffer.from('string', 'base64') - Stack Overflow
node.js - Nodejs InputStream Buffer convert to string using python - Stack Overflow
node.js - Buffer to string difference in NodeJs Vs Python - Stack Overflow
How about this?
var responseData = Buffer.from(x, 'utf8');
from: Convert string to buffer Node
Without digging very deep into your code, it seems to me that you might want to change
var responseData=x.toString();
to
var responseData=x.toString("binary");
and finally
response.write(new Buffer(toTransmit, "binary"));
The Buffer.toString() method accepts start and end parameters, which you can use to slice out just the subset you want for your substring. This may, depending on your implementation, be faster than allocation a new intermediary Buffer like you suggested in your answer.
Check out Node's Buffer.toString() method for more information.
I know I'm a bit late to this, but why not just something like:
buffer.subarray(0, bytes).toString();
How about this?
var responseData = Buffer.from(x, 'utf8');
from: Convert string to buffer Node
Without digging very deep into your code, it seems to me that you might want to change
var responseData=x.toString();
to
var responseData=x.toString("binary");
and finally
response.write(new Buffer(toTransmit, "binary"));