Buffer.from()
Creates a Buffer from a string, array, or another buffer.
Syntax
Buffer.from(value[, encodingOrOffset[, length]]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
value | string | Array | ArrayBuffer | Buffer | Yes | The source data to copy into a new Buffer. |
encoding | string | No | For string input, the encoding (default `'utf8'`); also `'base64'`, `'hex'`, `'latin1'`. |
Returns
Buffer — A new Buffer containing the data.
Examples
const b = Buffer.from('héllo', 'utf8');
console.log(b.length, b.toString('hex'));
Output
6 68c3a96c6c6f
const decoded = Buffer.from('aGVsbG8=', 'base64');
console.log(decoded.toString());
Output
hello
Notes
Use `Buffer.from`, never the unsafe `new Buffer()` constructor
(deprecated, can expose uninitialized memory). String byte length
differs from character length for multibyte text.