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.
In Node.js, a Buffer stores binary data as a sequence of bytes. When you pass an array to Buffer.from(), each array item is treated as one byte value and copied into a new Buffer.
In this tutorial, we will learn how to convert array to buffer using Buffer.from() method, with some examples. We will also cover byte ranges, boolean values, typed arrays, ArrayBuffer conversion, and common mistakes when converting JavaScript arrays to Node.js buffers.
Syntax – Buffer.from() to Convert an Array to Buffer
The syntax of from() method is
Buffer.from(array)
Buffer.from method reads octets from array and returns a buffer initialized with those read bytes.
The input array should contain byte values. A byte value is normally an integer from 0 to 255. The returned Buffer has the same length as the input array because each array element becomes one byte in the Buffer.
const buffer = Buffer.from([byte1, byte2, byte3]);
Use this form when you already have raw byte values, such as file bytes, network packet bytes, RGB channel values, binary protocol fields, or byte data received from another API.
Example 1 – Read an Octet Array to Buffer
In the following example, an octet array is read to a buffer.
array-to-buffer.js
var arr = [0x74, 0x32, 0x91];
const buf = Buffer.from(arr);
for(const byt of buf.values()){
console.log(byt);
}
Output
$ node array-to-buffer.js
116
50
145
We have logged data in each byte as a number.
0x74 = 0111 0100 = 116
0x32 = 0011 0010 = 50
0x91 = 1001 0001 = 145
The hexadecimal values are converted to their decimal byte values when printed. The Buffer itself still stores the same byte sequence.
Example 2 – Read a Number Array to Buffer
In the following example, a number array is read to a buffer.
array-to-buffer.js
var arr = [74, 32, 91];
const buf = Buffer.from(arr);
for(const byt of buf.values()){
console.log(byt);
}
Output
$ node array-to-buffer.js
74
32
91
We have logged data in each byte as a number.
The Buffer created from [74, 32, 91] contains three bytes. Its length is also three.
const arr = [74, 32, 91];
const buf = Buffer.from(arr);
console.log(buf.length);
Output
3
Example 3 – Read Boolean Array to Buffer
In the following example, an octet array is read to a buffer.
array-to-buffer.js
var arr = [true, true, false];
const buf = Buffer.from(arr);
for(const byt of buf.values()){
console.log(byt);
}
Output
$ node array-to-buffer.js
1
1
0
true is 1, while false is 0.
Although this works because boolean values are converted to numbers, it is usually clearer to pass numeric bytes directly when the Buffer represents binary data.
Byte Value Rules When Creating a Buffer from an Array
For predictable results, keep every value in the array within the byte range 0 to 255. If a value is outside this range, Node.js coerces it into a byte value. This can produce results that are correct according to byte conversion rules, but confusing in application code.
const arr = [255, 256, 257, -1];
const buf = Buffer.from(arr);
console.log([...buf]);
Output
[ 255, 0, 1, 255 ]
Here, 256 becomes 0, 257 becomes 1, and -1 becomes 255. When converting an array to a Buffer, validate the input if values may come from user input or another system.
Convert a Uint8Array to Buffer in Node.js
If the source data is already a Uint8Array, you can pass it to Buffer.from(). This is common when working with binary APIs, browser-compatible code, or data produced by typed array operations.
const bytes = new Uint8Array([72, 101, 108, 108, 111]);
const buf = Buffer.from(bytes);
console.log(buf);
console.log(buf.toString('utf8'));
Output
<Buffer 48 65 6c 6c 6f>
Hello
In this example, the numbers represent UTF-8 bytes for the string Hello. The Buffer can be converted back to text using toString('utf8').
Convert an ArrayBuffer to Buffer in Node.js
An ArrayBuffer is a generic JavaScript object for storing raw binary data. Node.js Buffer can be created from an ArrayBuffer using Buffer.from(arrayBuffer).
const arrayBuffer = new ArrayBuffer(4);
const view = new Uint8Array(arrayBuffer);
view[0] = 10;
view[1] = 20;
view[2] = 30;
view[3] = 40;
const buf = Buffer.from(arrayBuffer);
console.log([...buf]);
Output
[ 10, 20, 30, 40 ]
Use this approach when the input is an ArrayBuffer rather than a normal JavaScript array. If you already have a normal array like [10, 20, 30, 40], Buffer.from(array) is enough.
Convert an Array of Character Codes to a Readable String Buffer
Sometimes an array contains character codes. In that case, create the Buffer from the array and then call toString() with the correct encoding.
const codes = [78, 111, 100, 101];
const buf = Buffer.from(codes);
console.log(buf.toString('utf8'));
Output
Node
This works when the array contains valid bytes for the chosen encoding. For text data, UTF-8 is commonly used.
Buffer.from(array) vs Buffer.from(string)
Buffer.from(array) and Buffer.from(string) both create a Buffer, but they interpret the input differently.
| Input | Example | How Buffer.from() reads it |
|---|---|---|
| Array | [65, 66, 67] | Each item is used as one byte. |
| String | 'ABC' | The string is encoded into bytes using the selected encoding. |
| ArrayBuffer | new ArrayBuffer(3) | The raw bytes from the ArrayBuffer are used. |
| Uint8Array | new Uint8Array([65, 66, 67]) | The typed array bytes are copied into the Buffer. |
Choose the input form based on the data you already have. If your source is text, pass a string. If your source is byte values, pass an array or typed array.
Common Mistakes When Converting Array to Buffer in Node.js
- Do not pass nested objects or complex objects directly to
Buffer.from(array). Convert them to a string format such as JSON first, if text storage is required. - Do not assume array values outside
0to255will be stored as written. They are converted to byte values. - Do not use
Buffer.from(array)for a string array like['a', 'b']when you want text. Join or encode the string first. - Do not confuse
ArrayBufferwith a normal JavaScript array. Both can be used, but they are different input types. - Use the correct encoding when converting a Buffer back to a string with
toString().
Node.js Array to Buffer FAQs
How do you convert an array to a Buffer in Node.js?
Use Buffer.from(array). For example, Buffer.from([65, 66, 67]) creates a Buffer with three bytes.
What values should an array contain before converting it to a Buffer?
The array should normally contain integers from 0 to 255. Each value becomes one byte in the Buffer.
Can I convert an ArrayBuffer to a Node.js Buffer?
Yes. Use Buffer.from(arrayBuffer). If you need to fill or inspect the bytes first, create a typed array view such as Uint8Array over the ArrayBuffer.
Can I convert an array of objects to Buffer?
Do not pass an array of objects directly when you want meaningful object data. Convert the object or array to JSON first using JSON.stringify(), and then create a Buffer from that string.
How do I convert a Buffer created from an array back to an array?
You can use the spread operator or Array.from(). For example, [...buf] or Array.from(buf) returns the byte values as a normal array.
Editorial QA Checklist for Node.js Array to Buffer Conversion
- Confirm that examples use
Buffer.from(array)instead of the deprecatednew Buffer()constructor. - Check that byte values are explained as integers in the range
0to255. - Verify that array, typed array, and
ArrayBufferinputs are not described as the same thing. - Keep JavaScript code blocks marked with
language-javascriptand output blocks marked separately. - Explain object-array conversion through JSON when the goal is to preserve structured data.
Conclusion: Creating Node.js Buffers from Arrays
In this Node.js Tutorial – Node.js Convert Array to Buffer, we have learnt how to convert octet array, number array and boolean array to Node.js buffers.
Use Buffer.from(array) when you have byte values in an array. Use Buffer.from(arrayBuffer) for raw ArrayBuffer data, and use Buffer.from(string, encoding) when the source data is text.
TutorialKart.com