# Convert 2 bytes in a buffer to integer

**URL:** https://discourse.nodered.org/t/convert-2-bytes-in-a-buffer-to-integer/48354
**Category:** General
**Created:** [13 July 2021 10:32 UTC](https://discourse.nodered.org/t/convert-2-bytes-in-a-buffer-to-integer/48354 "2021-07-13T10:32:04Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Athurian](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Athurian](https://discourse.nodered.org/u/Athurian)
#### Post date: [13 July 2021 10:32 UTC](https://discourse.nodered.org/t/convert-2-bytes-in-a-buffer-to-integer/48354/1 "2021-07-13T10:32:04Z")

</div>

Please inquire about converting numbers.

Here is the buffered value.  
[1,3,10,1,4] \> msg.payload

Create a new buffer and truncate the item located at [3],[4]  
var pay1= Buffer.alloc(2);  
pay1[0]=msg.payload[3].slice(0,2)  
pay1[0]=msg.payload[4].slice(0,2)

Then slice it again and turn it into a string.  
var pay2=pay1.slice(0,4).toString('hex');

Then you get something like this:  
"0104"

So we convert this value to a number.  
var pay3=Number(pay2);

Then it comes out in numeric form.  
It comes out as 104 or 0x68.

the value i want is  
It should come out as 0x104, the number 260.  
But I keep getting 104 or 0x68.

Is there any way to solve this?

The code is organized as follows.

buffer [1,3,10,1,4]

var pay1= Buffer.alloc(2);  
pay1[0]=msg.payload[3].slice(0,2);  
pay1[0]=msg.payload[4].slice(0,2);  
var pay2=pay1.slice(0,4).toString('hex');  
var pay3=Number(pay2);  
msg.payload=pay3;  
return msg;

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [13 July 2021 11:05 UTC](https://discourse.nodered.org/t/convert-2-bytes-in-a-buffer-to-integer/48354/2 "2021-07-13T11:05:19Z")

</div>

That's how NodeJS Buffer.toString('hex') works. It's output is a hex string, but without the leading '0x'. By coincidence, your number doesn't contain digits with a to f, so the Number conversions thinks it's a decimal.

If you want to convert a Buffer part into a number, I suggest you use one of the many conversion functions, like `readInt16LE([offset])`. It's much easier and less error-prone.

See the docs for more methods like that: [Buffer | Node.js v14.17.3 Documentation](https://nodejs.org/dist/latest-v14.x/docs/api/buffer.html#buffer_buf_readint16le_offset)

Alternatively, there's an excellent node with does Buffer conversions for you:

> **[node-red-contrib-buffer-parser](https://flows.nodered.org/node/node-red-contrib-buffer-parser)**
>
> Node-red nodes to convert values to and from buffer/array. Supports Big/Little Endian, BCD, byte swapping and much more

---

<div class="post-metadata">

### Author: ![Athurian](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Athurian](https://discourse.nodered.org/u/Athurian)
#### Post date: [16 July 2021 01:41 UTC](https://discourse.nodered.org/t/convert-2-bytes-in-a-buffer-to-integer/48354/3 "2021-07-16T01:41:55Z")

</div>

thank you for the reply.

I solved the problem in another way.

I solved it as below.

var pay2=Buffer.alloc(2);  
pay2[0]=msg.payload[3];  
pay2[1]=msg.payload[4];  
var pay3=pay2.slice(0,4).toString('hex');  
var pay4=Number("0x"+pay3);

---

<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: [16 July 2021 07:01 UTC](https://discourse.nodered.org/t/convert-2-bytes-in-a-buffer-to-integer/48354/4 "2021-07-16T07:01:10Z")

</div>

> [@Athurian](#):
>
> the value i want is  
> It should come out as 0x104, the number 260.

As @kuema suggested, this is far easier with buffer-parser...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/8/58b3e971a4e7efc669f6d87cc1984dc1fb0249a2.png)

```auto
[{"id":"8f6091afbdeee1b0","type":"inject","z":"1683bd9a.5e0a02","name":"[1,3,10,1,4]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[1,3,10,1,4]","payloadType":"bin","x":770,"y":140,"wires":[["029ef0aab35cc3b7"]]},{"id":"029ef0aab35cc3b7","type":"buffer-parser","z":"1683bd9a.5e0a02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"int16be","name":"item1","offset":3,"length":1,"offsetbit":0,"scale":"1","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"output","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":950,"y":140,"wires":[["24254f481d8d7fbf"]]},{"id":"24254f481d8d7fbf","type":"debug","z":"1683bd9a.5e0a02","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1120,"y":140,"wires":[]}]

```

If you insist on DIY then you should probably fix up your function code. There is no need to convert to hex string & append '0x0' and then parse it back to an integer.

Combining bytes to make an integer is as simple as shifting the one byte 8 places left then OR the 2 bytes...

```auto
msg.payload = msg.payload[3] << 8 | msg.payload[4];
return msg;

```

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

---

<div class="post-metadata">

### Author: ![Athurian](https://avatars.discourse-cdn.com/v4/letter/a/dbc845/32.png) [@Athurian](https://discourse.nodered.org/u/Athurian)
#### Post date: [16 July 2021 08:34 UTC](https://discourse.nodered.org/t/convert-2-bytes-in-a-buffer-to-integer/48354/5 "2021-07-16T08:34:37Z")

</div>

thank you for the reply.  
It is not possible to do it in the Buffer Node because some modifications have to be made in the function code.

We will refer to it next time.

---

<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: [2 August 2021 10:50 UTC](https://discourse.nodered.org/t/convert-2-bytes-in-a-buffer-to-integer/48354/7 "2021-08-02T10:50:18Z")

</div>

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