# Hex to Base64 function

**URL:** <https://discourse.nodered.org/t/hex-to-base64-function/31864>\
**Category:** General\
**Created:** [22 August 2020 03:18 UTC](https://discourse.nodered.org/t/hex-to-base64-function/31864 "2020-08-22T03:18:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vangalvin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vangalvin/32/27213_2.png) [@vangalvin](https://discourse.nodered.org/u/vangalvin)\
**Post date:** [22 August 2020 03:18 UTC](https://discourse.nodered.org/t/hex-to-base64-function/31864/1 "2020-08-22T03:18:42Z")

</div>

The project I am working on requires me to set the ID and channel data of a device.  
Both are in HEX, I managed to get the ID data working fine but the channel data is an issue  
becaue its a 16bit hex value

0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000  
Node Red is viving me "/wAAAAAA"  
And it should be "AP8AAAAAAAAAAAAA"

This is how im doing it in the function

```auto
let buff = new Buffer([0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000]);
let base64data = buff.toString('base64');
msg.payload.data = base64data;

```

Im assuming the reason for this is that the buffer is only 8bit and it needs to be 16bit?

---

<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:** [22 August 2020 05:07 UTC](https://discourse.nodered.org/t/hex-to-base64-function/31864/2 "2020-08-22T05:07:49Z")

</div>

Hi @vangalvin

Couple of points...

- You cannot initialise a Buffer with 16 bit data.
  - as you suspected, it is truncating your integers to bytes

- Also, `new Buffer` is depreciated (so wont work in the future anyhow)
  - use Buffer.from(...) instead.

So two ways:

1: You can iterate the 16 bit data and write it to a buffer using the [buffer functions](https://nodejs.org/api/buffer.html#buffer_buf_writeuint16le_value_offset) e.g. `buf.writeUInt16LE()` then call `toString("base64")`.

2: Alternatively, you could use a Uint16Array as an intermediate variable for conversion ...

```auto
var data = [0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000];
var arr = new Uint16Array(data);
var buf = Buffer.from(arr.buffer);
buf.swap16();
msg.payload = {
    srcData: data,
    asBuffer: buf,
    asBase64: buf.toString("base64")
}
return msg;

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/0/50882f7f43152e1c3d634f59100796f4a4384917.png)

---

<div class="post-metadata">

**Author:** ![vangalvin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vangalvin/32/27213_2.png) [@vangalvin](https://discourse.nodered.org/u/vangalvin)\
**Post date:** [22 August 2020 05:44 UTC](https://discourse.nodered.org/t/hex-to-base64-function/31864/3 "2020-08-22T05:44:05Z")

</div>

That works a treat thanks!  
Is there a way to concatinate the buffer as well?

Initially I was going to allow each part to be set indavidually, Like the devEui and appEui but thinking about it this would end up being a pain.

```auto
var devEui = [0x32, 0x42, 0x33, 0x00, 0x00, 0x88, 0x88, 0x06];
var appEui = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
var appKey = [0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01];
var data = [0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000];

var arr = new Uint16Array(data);
var buf = Buffer.from(arr.buffer);

buf.swap16();
msg.payload = {
    srcData: data,
    asBuffer: buf,
    asBase64: buf.toString("base64")
}
return msg;

```

The output should end up something like  
MkIzAACIiAYAAAAAAAAAAIiIiIiIiIiIiIiIiIiIZgEA/wAAAAAAAAAAAAA=

Or would I be better off sending them in seperate packets?

---

<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:** [22 August 2020 06:09 UTC](https://discourse.nodered.org/t/hex-to-base64-function/31864/4 "2020-08-22T06:09:11Z")

</div>

> [@vangalvin](#):
>
> Is there a way to concatinate the buffer as well?

Sure see the [buffer documentation](https://nodejs.org/api/buffer.html#buffer_static_method_buffer_concat_list_totallength)

```auto
var devEui = Buffer.from( [0x32, 0x42, 0x33, 0x00, 0x00, 0x88, 0x88, 0x06] );
var appEui = Buffer.from( [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00] );
var appKey = Buffer.from( [0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x66, 0x01] );

var data = [0x00FF,0x0000,0x0000,0x0000,0x0000,0x0000];
var arr = new Uint16Array(data);
var buf = Buffer.from(arr.buffer);
buf.swap16();

const totalLength = buf.length + devEui.length + appEui.length + appKey.length;

var finalBuffer = Buffer.concat([devEui, appEui, appKey, buf], totalLength);

msg.payload = {
    srcData: data,
    asBuffer: buf,
    asBase64: buf.toString("base64"),
    finalBuffer: finalBuffer,
    finalBufferB64: finalBuffer.toString("base64")
}
return msg;

```

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

---

<div class="post-metadata">

**Author:** ![vangalvin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/vangalvin/32/27213_2.png) [@vangalvin](https://discourse.nodered.org/u/vangalvin)\
**Post date:** [22 August 2020 06:22 UTC](https://discourse.nodered.org/t/hex-to-base64-function/31864/5 "2020-08-22T06:22:34Z")

</div>

Awesome, thanks for that and I see what i was doing wrong when i tried to do it..  
var finalBuffer = Buffer.concat([devEui, appEui, appKey, buf], totalLength);

not the way I was trying to do it. 🙂  
var finalBuffer = Buffer.concat(devEui, appEui, appKey, buf);

Now I just have to hope the data rates are up to the task. If the radio drops below DR3 they its limited to 51 bytes ☹

Thanks you so much for you help with this  
:

---

<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:** [5 September 2020 06:22 UTC](https://discourse.nodered.org/t/hex-to-base64-function/31864/6 "2020-09-05T06:22:40Z")

</div>

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