Answers for "node print buffer as string"

2

javascript string to buffer

var buf = Buffer.from('abc');

console.log(buf.toString());
Posted by: Guest on March-13-2021
0

buffer to string

Your code is working. The buffer you have is actually the string "[object Object]".

let b = Buffer.from('[object Object]', 'utf8')
console.log(JSON.stringify(b))
// {"type":"Buffer","data":[91,111,98,106,101,99,116,32,79,98,106,101,99,116,93]}

console.log(b.toString('utf8'))
// [Object object]
The problem you need to figure out is why is a buffer with that string being sent. It seems like the sender of the buffer needs to call stringify or otherwise serialize the object before sending it. Then you can turn it back to a string with toString() and use JSON.parse() on the string.
Posted by: Guest on March-11-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language