# Convert buffer to uInt8array

**URL:** <https://discourse.nodered.org/t/convert-buffer-to-uint8array/30770>\
**Category:** General\
**Created:** [27 July 2020 13:08 UTC](https://discourse.nodered.org/t/convert-buffer-to-uint8array/30770 "2020-07-27T13:08:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmoscolini](https://avatars.discourse-cdn.com/v4/letter/v/d6d6ee/32.png) [@vmoscolini](https://discourse.nodered.org/u/vmoscolini)\
**Post date:** [27 July 2020 13:08 UTC](https://discourse.nodered.org/t/convert-buffer-to-uint8array/30770/1 "2020-07-27T13:08:55Z")

</div>

Hi guys,

I am using node-red to implement an ap and I am racking my brains to figure out how to a buffer to uint8array (I am not sure that is that type).

The node "jimp-image" of module "node-red-contrib-image-tools" outputs an image as a buffer or a base64 string. I need to pass the responsed image into a web service which requires an uint8array base image.

Below is the output buffer format from jimp-image:

![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a5c0b79957531451d87186968ed36fd48c0b647.png)

But I need the following type of buffer:

![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08026c68f136b83b558814ece4ac036f9f7dc504.png)

Do you have any idea how to convert the buffer to an array buffer as showed into the attached image?

Thank you very much for any support you can provide.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 July 2020 13:27 UTC](https://discourse.nodered.org/t/convert-buffer-to-uint8array/30770/2 "2020-07-27T13:27:01Z")

</div>

> [@vmoscolini](#):
>
> I need to pass the responsed image into a web service which requires an uint8array base image

[Instances of `Buffer` are also instances of `Uint8Array`](https://nodejs.org/docs/latest/api/buffer.html#buffer_buffers_and_typedarray) in node.js 4.x and higher. Thus, the most efficient solution is to access the `buf.buffer` property directly, as per [node.js - Convert a binary NodeJS Buffer to JavaScript ArrayBuffer - Stack Overflow](https://stackoverflow.com/a/31394257/1375574)

---

<div class="post-metadata">

**Author:** ![vmoscolini](https://avatars.discourse-cdn.com/v4/letter/v/d6d6ee/32.png) [@vmoscolini](https://discourse.nodered.org/u/vmoscolini)\
**Post date:** [27 July 2020 14:56 UTC](https://discourse.nodered.org/t/convert-buffer-to-uint8array/30770/3 "2020-07-27T14:56:21Z")

</div>

Hi Steve,  
Thank you for your prompt reply.

Looking into the stackoverflow post you sent, what I want is exactly the opposite of this answer...

> <https://stackoverflow.com/questions/8609289/convert-a-binary-nodejs-buffer-to-javascript-arraybuffer/17064149#17064149>

> "From ArrayBuffer to Buffer" could be done this way:  
> `var buffer = Buffer.from( new Uint8Array(ab) );`

Do you have any idea how to do it, from Buffer to ArrayBuffer?

Thanks,

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 July 2020 15:12 UTC](https://discourse.nodered.org/t/convert-buffer-to-uint8array/30770/4 "2020-07-27T15:12:34Z")

</div>

I dont know where you were reading?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1de2ed86ca1c975a1c8f7faeeaeca0ba8eb316c2.png)

demo...  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/91b86bc2399a22151157183eec1724e6c69269ef.png)

try adding this to a function node after the image-tools output / before the http request...

```auto
let b = msg.payload; //get the image buffer
msg.payload = b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
return msg;

```

---

<div class="post-metadata">

**Author:** ![vmoscolini](https://avatars.discourse-cdn.com/v4/letter/v/d6d6ee/32.png) [@vmoscolini](https://discourse.nodered.org/u/vmoscolini)\
**Post date:** [27 July 2020 16:05 UTC](https://discourse.nodered.org/t/convert-buffer-to-uint8array/30770/5 "2020-07-27T16:05:32Z")

</div>

Hi Steve,

Sorry but this answer does not work.  
It always returns an empty object not an array.

 ![3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4cc10dc82862536d48a3db1dac30755cc2015db7.png)

Am I doing something wrong?

Thank you for yor help.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [27 July 2020 17:14 UTC](https://discourse.nodered.org/t/convert-buffer-to-uint8array/30770/6 "2020-07-27T17:14:01Z")

</div>

> [@vmoscolini](#):
>
> It always returns an empty object not an array.

No it's not. It's an ArrayBuffer. It states that in your screenshot. It's just that the debug output doesn't show you the contents like it does for a buffer.

Also, why have you used the hardcored buffer and not the payload from the image?

I have a feeling you don't know precisely what you are supposed to send to this API. It is quite possible a buffer sent with the correct headers etc will work perfectly fine.

Can you clarify that. Perhaps a link to documentation?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 September 2020 17:14 UTC](https://discourse.nodered.org/t/convert-buffer-to-uint8array/30770/7 "2020-09-25T17:14:14Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
