Instances of Buffer are also instances of Uint8Array in node.js 4.x and higher. Thus, the most efficient solution is to access the buffer's own .buffer property directly, as per https://stackoverflow.com/a/31394257/1375574. The Buffer constructor also takes an ArrayBufferView argument if you need to go the other direction.

Note that this will not create a copy, which means that writes to any ArrayBufferView will write through to the original Buffer instance.


In older versions, node.js has both ArrayBuffer as part of v8, but the Buffer class provides a more flexible API. In order to read or write to an ArrayBuffer, you only need to create a view and copy across.

From Buffer to ArrayBuffer:

function toArrayBuffer(buffer) {
  const arrayBuffer = new ArrayBuffer(buffer.length);
  const view = new Uint8Array(arrayBuffer);
  for (let i = 0; i < buffer.length; ++i) {
    view[i] = buffer[i];
  }
  return arrayBuffer;
}

From ArrayBuffer to Buffer:

function toBuffer(arrayBuffer) {
  const buffer = Buffer.alloc(arrayBuffer.byteLength);
  const view = new Uint8Array(arrayBuffer);
  for (let i = 0; i < buffer.length; ++i) {
    buffer[i] = view[i];
  }
  return buffer;
}
Answer from Martin Thomson on Stack Overflow
🌐
Node.js
nodejs.org › api › buffer.html
Buffer | Node.js v26.11.1 Documentation
When creating a Buffer using a <TypedArray>'s .buffer, it is possible to use only a portion of the underlying <ArrayBuffer> by passing in byteOffset and length parameters.
🌐
Bun
bun.com › binary data › convert an arraybuffer to a buffer
Convert an ArrayBuffer to a Buffer - Bun
July 25, 2026 - Use the static Buffer.from() method to create a Buffer from an ArrayBuffer.
Discussions

ELI5: File, Uint8Array, Buffer, Arraybuffer, BytesArray, Blob…
A digital system, like a computer works on digital data, thus things which can be represented as digits. Or simpler: Computer sees a bit of numbers and works on them. Also a text is just a long number. Some time ago, one decided to have computers work on binary digits and to group 8 out of those as the typical size. That is a byte. (There were computers using other byte sizes, that's why sometimes, especially in network protocols, it is called octet) With a byte of eight bits you can represent 256 values. (From 0 to 255) over multiple decades there were discussions on representing text (actually that was prr-computer already with telegraphs etc) and one found the ASCII rules to map between numeric values and characters. Each letter in the English alphabet got a code. Thus the word Hello becomes the sequence of numbers 72 101 108 108 111 Now decimal system ia nice when counting with fingers, but not so good for a binary machine. As said we have the byte, thus instead ten digits is inconvenient so we use a number system with base 16, thus any digit can not only have ten different values (0 to 9) but 16 (0 to 9 and A to F) Thus the same numbers as above can be written in hex 0x48 0x65 0x6C 0x6C 0x6F Now each byte is represented by two hex digits which gives a nice structure (and if you notice that 0x41 is A and 0x61 is a you start to understand ASCII) Now over time 256 characters wasn't enough for represting text in all languages, so after many years and messing around with different code pages and encodings smart folks came up with Unicode and Utf-8 to encode all characters. Won't go there too deep, but in essence utf-8 uses the ASCII table for the first 128 characters (half a byte, the lower half) and all other characters use multiple bytes. The German letter Ä fornisntance is 0xC3 0x48. In addition the Unicode standard has specific rules on sorting and comparing those values and some fancy transformations. (Sorting rules are even language dependent, so the German Ä can be sorted like AE, behind Z or simply as A) Wenn Browsers and JavaScript were created it was decided that a string in JavaScirpt shall be Unicode data. This is logical as websites are full of human readable text and properties of Unicode are good (well, the story is more complicated there, but good enough) This was good for the time where Web was working with simple websites and JavaScript was doing minor text manipulation. However for lower layers of the compitong stack data is just a sequence of bytes. In memory you have bytes. A file is a sequence of bytes. The meaning of those bytes might be Unicode text, might be an image, might even be bianry program code all are just bytes. The difference is only in the one looking at it. Now treating all data as unicode string will do harm, as they have all the assumptions on top where only specific byte sequences are valid and so on. So something else had to be added. And in fact different groups added different things. Before we can explain that, one step more is needed: as said interpretation depends on who looks at the data. So back to our numbers and from decimal and hex to binary. As said 8 bit are a byte. The number 1 in binary with it 8 bits would look like 0b00000001 Here using 0b as prefix, so you interpret it as binary, not hey, not decimal. The value 127 inn inary looks like this: 0b01111111 all bits, but the first set. Now something fancy happens when we set the first bit to 1 as well: 0b11111111 This can either be 255 or -1. Wait what negative!? You will ask: Yes, there eisna problem with these numbers: Our byte has to represent negative numbers as well. And then the first digit tells us whether it's negative or positive. And whenever we look at the byte we have to make a choice how we look at it. Is it a text character? Is it a positive number? A negative number? The byte doesn't know and doesn't care only we when looking at it. Now the Uint8Array enters! By giving a sequence of bytes (some area in memory) to an Uint8Buffer we tell it "look at those bytes and treat tit like a sequence of bytes, where each byte is a 8 bit unsigned number" and by that we can work with those.bytes and each byte will be a number 0 to 256 and whatever we do with the array decides on what it does with those bytes. Maybe it is a pixel in an image, maybe an frequency on an audio file, maybe ... Now another term there eisnthe Buffer. A buffer is just some area in memory. An arrayBuffer is "this is my area of memory, but I haven't yet decided how i will interpret it" I said Buffer, I said complicazednhsorory ... That tis that Node.js developers Hand this issue before ArrayBuffer d Uint8Array was defined, so they created their own thing, which is similar to an Unit8Array (modern versions of Buffer are built on Uint8Array) but don't be confused, buffer is a generic term for some data stored somewhere, which is distinct from Buffer, which is a type in Node.js. Now there ris a bit more. 256 is a bit low formmany purposes. So you can group multiple bytes and make them digits in a larger number. Quite typical are efor 4 byte, which are 32 bit. Thus a Int32Array takes 4 bytes at once from the underlying data (the ArrayBuffer, the buffer, the memory region) and makes it a 32 bit signed number (- 2147483648 to 2147483647) the Uint32Array (0 to 4294967295) and if that isn't enought there are big(u)int64Array, which look at 8 byte at a time, which is also the number of byte/bit a modern CPU can store in it's register, transfer though bus and process the best, hence 64bit architecture. The key thing is: memory (RAM, disk or any other form) in modern computers is just a sequence of bytes and the way you look at it decides on the meaning. The computer doesn't know and you have to tell it. Uint8Array is the typically representation for low level work. ArrayBuffer is when you haven't decided, yet and all else are more special. More on reddit.com
🌐 r/node
6
12
December 16, 2021
feature: support initializing Buffer from an ArrayBuffer
We have the buffer npm module which gives us the same Buffer API in the browser (and is used by browserify), however it tracks the node.js/io.js buffer exactly, so we can't add support for new Buffer(arraybuffer) unless core does too. More on github.com
🌐 github.com
31
December 8, 2014
JS: How to convert ArrayBuffer to string?
Convert it to a Uint8Array, then convert that to a base64 string with btoa: https://stackoverflow.com/questions/9267899/arraybuffer-to-base64-encoded-string More on reddit.com
🌐 r/learnprogramming
1
1
May 2, 2023
Base64 to ArrayBuffer conversion is generating corrupted image file
This post helped me solving the problem. const imageBase64Str = req.body.logo.replace(/^.+,/, '') const buf = Buffer.from(imageBase64Str, 'base64') More on reddit.com
🌐 r/node
4
0
February 12, 2023
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › ArrayBuffer
ArrayBuffer - JavaScript - MDN Web Docs
It is an array of bytes, often ... of an ArrayBuffer; instead, you create one of the typed array objects or a DataView object which represents the buffer in a specific format, and use that to read and write the contents of the buffer....
🌐
npm
npmjs.com › package › arraybuffer-to-buffer
arraybuffer-to-buffer - npm
August 1, 2019 - var arrayBufferToBuffer = require('arraybuffer-to-buffer'); var ab = new ArrayBuffer(12); var v = new DataView(ab); [].slice.call('abc').forEach(function(s, i) { v[i] = s.charCodeAt(0); }); var b = arrayBufferToBuffer(ab); b.toString('utf8', 0, 3); // 'abc' MIT ·
      » npm install arraybuffer-to-buffer
    
Published: Aug 01, 2019
Version: 0.0.7
Author: Miguel Mota
🌐
GitHub
gist.github.com › miguelmota › 5b06ae5698877322d0ca
Node.js Buffer to ArrayBuffer · GitHub
function toArrayBuffer(buffer) { return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength); } function toBuffer(byteArray) { return Buffer.from(byteArray); }
🌐
GitHub
miguelmota.com › bytes › arraybuffer-to-buffer
miguelmota (miguel) · GitHub
December 29, 2015 - miguelmota has 550 repositories available. Follow their code on GitHub.
Find elsewhere
🌐
Node.js
nodejs.org › download › release › v10.2.0 › docs › api › buffer.html
Buffer | Node.js v10.2.0 Documentation
Starting in Node.js 8.0.0, Buffer(num) ... argument copies the passed object's data into the Buffer. Passing an ArrayBuffer or a SharedArrayBuffer returns a Buffer that shares allocated memory with the given array buffer....
🌐
Node.js
nodejs.org › download › release › v9.10.1 › docs › api › buffer.html
Buffer | Node.js v9.10.1 Documentation
Starting in Node.js 8.0.0, Buffer(num) ... argument copies the passed object's data into the Buffer. Passing an ArrayBuffer or a SharedArrayBuffer returns a Buffer that shares allocated memory with the given array buffer....
🌐
Medium
dev-aditya.medium.com › buffer-arraybuffer-float64array-in-node-js-ef68c112d905
Buffer, ArrayBuffer, Float64Array in Node.js | by Aditya Yadav | Medium
September 29, 2024 - It provides a way to handle and manipulate binary data as floating-point numbers. Element Type: 64-bit floating-point (double) numbers. Endianess: Platform-dependent (usually little-endian). Typed Array Features: Provides methods for accessing and modifying binary data efficiently. // Create an ArrayBuffer of 16 bytes const buffer = new ArrayBuffer(16); // Create a Float64Array view const float64View = new Float64Array(buffer); // Assign values float64View[0] = Math.PI; float64View[1] = Math.E; console.log(float64View); // Output: Float64Array(2) [ 3.141592653589793, 2.718281828459045 ]
🌐
techtutorialsx
techtutorialsx.wordpress.com › 2019 › 10 › 27 › node-js-converting-array-to-buffer
Node.js: Converting Array to Buffer – techtutorialsx
October 27, 2019 - Then, to convert the array to a Buffer, we simply need to call the from class method, passing as input our array.
🌐
Educative
educative.io › answers › what-is-bufferfrom-in-nodejs
What is Buffer.from() in Node.js?
Note: The underlying array and the buffer share the same memory. The following example demonstrates how to use ArrayBuffer to create a buffer though the Buffer.from(arrayBuffer[, byteOffset[, length]]) function.
🌐
npm
npmjs.com › package › buffer-to-arraybuffer
buffer-to-arraybuffer - npm
February 21, 2021 - var bufferToArrayBuffer = require('buffer-to-arraybuffer'); var b = new Buffer(12); b.write('abc', 0); var ab = bufferToArrayBuffer(b); String.fromCharCode.apply(null, new Uint8Array(ab)); // 'abc' NOTE: If you only target node v4.3+, you can simply just do: new Buffer([12]).buffer ·
      » npm install buffer-to-arraybuffer
    
Published: Feb 21, 2021
Version: 0.0.6
🌐
Node.js
nodejs.org › download › release › v8.1.2 › docs › api › buffer.html
Buffer | Node.js v8.1.2 Documentation
Starting in Node.js 8.0.0, Buffer(num) ... as the first argument copies the passed object's data into the Buffer. Passing an ArrayBuffer returns a Buffer that shares allocated memory with the given ArrayBuffer....
🌐
Nocin
nocin.eu › nodejs-arraybuffer-to-buffer-uint8array-to-buffer
[nodejs] ArrayBuffer to Buffer | Uint8Array to Buffer – nocin.eu
Skip to content · nocin.eu Homelab, Linux, JS & ABAP (~˘▾˘)~ JavaScript / nodejs · June 8, 2023 June 8, 2023 arrayBuffer, buffer, nodejs, Uint8Array · https://nodejs.org/api/buffer.html · const buffer1 = Buffer.from(ArrayBuffer) const buffer2 = Buffer.from(Uint8Array) This site is best viewed using Dark Reader!
🌐
Reddit
reddit.com › r/node › eli5: file, uint8array, buffer, arraybuffer, bytesarray, blob…
r/node on Reddit: ELI5: File, Uint8Array, Buffer, Arraybuffer, BytesArray, Blob…
December 16, 2021 -

I’m always ending up guessing.. I need to somehow understand. I have no idea what I’m doing when working with files. Just trying stuff until it works. Someone plz ELI5..

My current issue is getting a png file from Firebase storage which gives a “file” which I’m trying to add on a pdf, where the library expects a Uint8Array or Arraybuffer. I’ve been doing similar things before. But end up just guessing my way until it works, and this time.. well it doesn’t work. I guessed it all with no success. I don’t know what I’m doing so I guess I need to actually learn ;)

Top answer
1 of 2
36
A digital system, like a computer works on digital data, thus things which can be represented as digits. Or simpler: Computer sees a bit of numbers and works on them. Also a text is just a long number. Some time ago, one decided to have computers work on binary digits and to group 8 out of those as the typical size. That is a byte. (There were computers using other byte sizes, that's why sometimes, especially in network protocols, it is called octet) With a byte of eight bits you can represent 256 values. (From 0 to 255) over multiple decades there were discussions on representing text (actually that was prr-computer already with telegraphs etc) and one found the ASCII rules to map between numeric values and characters. Each letter in the English alphabet got a code. Thus the word Hello becomes the sequence of numbers 72 101 108 108 111 Now decimal system ia nice when counting with fingers, but not so good for a binary machine. As said we have the byte, thus instead ten digits is inconvenient so we use a number system with base 16, thus any digit can not only have ten different values (0 to 9) but 16 (0 to 9 and A to F) Thus the same numbers as above can be written in hex 0x48 0x65 0x6C 0x6C 0x6F Now each byte is represented by two hex digits which gives a nice structure (and if you notice that 0x41 is A and 0x61 is a you start to understand ASCII) Now over time 256 characters wasn't enough for represting text in all languages, so after many years and messing around with different code pages and encodings smart folks came up with Unicode and Utf-8 to encode all characters. Won't go there too deep, but in essence utf-8 uses the ASCII table for the first 128 characters (half a byte, the lower half) and all other characters use multiple bytes. The German letter Ä fornisntance is 0xC3 0x48. In addition the Unicode standard has specific rules on sorting and comparing those values and some fancy transformations. (Sorting rules are even language dependent, so the German Ä can be sorted like AE, behind Z or simply as A) Wenn Browsers and JavaScript were created it was decided that a string in JavaScirpt shall be Unicode data. This is logical as websites are full of human readable text and properties of Unicode are good (well, the story is more complicated there, but good enough) This was good for the time where Web was working with simple websites and JavaScript was doing minor text manipulation. However for lower layers of the compitong stack data is just a sequence of bytes. In memory you have bytes. A file is a sequence of bytes. The meaning of those bytes might be Unicode text, might be an image, might even be bianry program code all are just bytes. The difference is only in the one looking at it. Now treating all data as unicode string will do harm, as they have all the assumptions on top where only specific byte sequences are valid and so on. So something else had to be added. And in fact different groups added different things. Before we can explain that, one step more is needed: as said interpretation depends on who looks at the data. So back to our numbers and from decimal and hex to binary. As said 8 bit are a byte. The number 1 in binary with it 8 bits would look like 0b00000001 Here using 0b as prefix, so you interpret it as binary, not hey, not decimal. The value 127 inn inary looks like this: 0b01111111 all bits, but the first set. Now something fancy happens when we set the first bit to 1 as well: 0b11111111 This can either be 255 or -1. Wait what negative!? You will ask: Yes, there eisna problem with these numbers: Our byte has to represent negative numbers as well. And then the first digit tells us whether it's negative or positive. And whenever we look at the byte we have to make a choice how we look at it. Is it a text character? Is it a positive number? A negative number? The byte doesn't know and doesn't care only we when looking at it. Now the Uint8Array enters! By giving a sequence of bytes (some area in memory) to an Uint8Buffer we tell it "look at those bytes and treat tit like a sequence of bytes, where each byte is a 8 bit unsigned number" and by that we can work with those.bytes and each byte will be a number 0 to 256 and whatever we do with the array decides on what it does with those bytes. Maybe it is a pixel in an image, maybe an frequency on an audio file, maybe ... Now another term there eisnthe Buffer. A buffer is just some area in memory. An arrayBuffer is "this is my area of memory, but I haven't yet decided how i will interpret it" I said Buffer, I said complicazednhsorory ... That tis that Node.js developers Hand this issue before ArrayBuffer d Uint8Array was defined, so they created their own thing, which is similar to an Unit8Array (modern versions of Buffer are built on Uint8Array) but don't be confused, buffer is a generic term for some data stored somewhere, which is distinct from Buffer, which is a type in Node.js. Now there ris a bit more. 256 is a bit low formmany purposes. So you can group multiple bytes and make them digits in a larger number. Quite typical are efor 4 byte, which are 32 bit. Thus a Int32Array takes 4 bytes at once from the underlying data (the ArrayBuffer, the buffer, the memory region) and makes it a 32 bit signed number (- 2147483648 to 2147483647) the Uint32Array (0 to 4294967295) and if that isn't enought there are big(u)int64Array, which look at 8 byte at a time, which is also the number of byte/bit a modern CPU can store in it's register, transfer though bus and process the best, hence 64bit architecture. The key thing is: memory (RAM, disk or any other form) in modern computers is just a sequence of bytes and the way you look at it decides on the meaning. The computer doesn't know and you have to tell it. Uint8Array is the typically representation for low level work. ArrayBuffer is when you haven't decided, yet and all else are more special.
2 of 2
1
it's a bit of a Pia esp when different libs want diff types. An ArrayBuffer is raw data. Like a big bag of data. You can't do anything with it - but if you convert it to something that can look inside the bag (Uint8Array), you can read / write it. If you just want to read, then Blob is your guy.
🌐
W3Schools
w3schools.com › nodejs › nodejs_buffer.asp
Node.js Buffer Module
Only use it when you understand the security implications and plan to immediately fill the entire buffer. Creates a new Buffer from various sources like strings, arrays, or ArrayBuffer.
🌐
GitHub
github.com › nodejs › node › issues › 106
feature: support initializing Buffer from an ArrayBuffer · Issue #106 · nodejs/node
December 8, 2014 - var u = new Uint8Array([1, 2, 3, 4]); var ab = u.buffer; var b = new Buffer(ab); console.log(b); // <Buffer > ... When writing isomorphic code (i.e. code that runs on the server and in the browser), it's often the case that you'll get an ArrayBuffer from a DOM API (xhr, websockets, webrtc, etc.) and need to convert it to a Buffer to work with modules in the npm ecosystem.
Author: nodejs