Node.js – Convert Array to Buffer

Node.js – Convert Array to Buffer : To convert array (octet array/ number array/ binary array) to buffer, use Buffer.from(array) method.

Syntax

Buffer.from(array)

Buffer.from method reads octets from array and returns a buffer initialized with those read bytes.

Example – Read an octet array to buffer

In the following example, an octet array is read to a buffer.

We have logged data in each byte as a number.

0x74 = 0111 0100 = 116 0x32 = 0011 0010 = 50 0x91 = 1001 0001 = 145

Example – Read a number array to buffer

In the following example, a number array is read to a buffer.

We have logged data in each byte as a number.

Example – Read boolean array to buffer

In the following example, an octet array is read to a buffer.

true is 1, while false is 0.

Conclusion :

In this Node.js TutorialNode.js Convert Array to Buffer, we have learnt how to convert octet array, number array and boolean array to Node.js buffers.