Answers for "new string buffer"

1

nodejs create buffer from string

// a string
const str = "Hey. this is a string!";

// convert string to Buffer
const buff = Buffer.from(str, "utf-8");

console.log(buff); // <Buffer 48 65 79 2e ... 72 69 6e 67 21>
Posted by: Guest on December-31-2021
0

Create buffers from strings using the Buffer.from() function. Like toString(), you can pass an encoding argument to Buffer.from().

let buf = Buffer.from('Hello, World', 'utf8');

buf.toString('hex'); // '48656c6c6f2c20576f726c64'
buf.toString('utf8'); // 'Hello, World'

buf = Buffer.from('48656c6c6f2c20576f726c64', 'hex');
buf.toString('utf8'); // 'Hello, World'
Posted by: Guest on May-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language