from
BufferConstructor.from
**from**(arrayBuffer, byteOffset?, length?): [Buffer](/references/product/variables/Buffer-1)
Allocates a new Buffer
using an array
of bytes in the range 0
– 255
.
Array entries outside that range will be truncated to fit into it.
If array
is an Array
-like object (that is, one with a length
property of
type number
), it is treated as if it is an array, unless it is a Buffer
or
a Uint8Array
. This means all other TypedArray
variants get treated as anArray
. To create a Buffer
from the bytes backing a TypedArray
, use Buffer.copyBytesFrom()
.
A TypeError
will be thrown if array
is not an Array
or another type
appropriate for Buffer.from()
variants.
Buffer.from(array)
and Buffer.from(string)
may also use the internalBuffer
pool like Buffer.allocUnsafe()
does.
Parameters
byteOffset
numberlength
numberReturns
Since
v5.10.0
**from**(data): [Buffer](/references/product/variables/Buffer-1)
Creates a new Buffer using the passed {data}
Parameters
data
Uint8Array | readonly number[]RequiredReturns
**from**(data): [Buffer](/references/product/variables/Buffer-1)
Parameters
Returns
**from**(str, encoding?): [Buffer](/references/product/variables/Buffer-1)
Creates a new Buffer containing the given JavaScript string {str}. If provided, the {encoding} parameter identifies the character encoding. If not provided, {encoding} defaults to 'utf8'.
Parameters
encoding
BufferEncoding