# How to form a new number with two bytes?

**URL:** <https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259>\
**Category:** General\
**Created:** [21 March 2019 03:01 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259 "2019-03-21T03:01:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![qpongoaka](https://avatars.discourse-cdn.com/v4/letter/q/edb3f5/32.png) [@qpongoaka](https://discourse.nodered.org/u/qpongoaka)\
**Post date:** [21 March 2019 03:01 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/1 "2019-03-21T03:01:51Z")

</div>

Hello everyone. I am facing the following problem:

In MQTT, i'm suscribe to a topic and i'm constantly receiving a frame consisting in three bytes like so: (SoF) ( byte1) (byte2).  
Where (SoF) represents the start of the frame ( it's just a char) and (byte1),(byte2) the most significant and less significant bits of a uint16.

My question is, how can I "sense" when a frame is comming and then concatenate the byte1 and the byte2 in a single unsigned int variable?

In C it's quite easy to do it, but since this is my first time programming in .js I have no clue of where to start.

Any help is more than wellcome.

---

<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:** [21 March 2019 07:13 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/2 "2019-03-21T07:13:36Z")

</div>

Try feeding your payload to a function node then shift byte1 right by 8 then add together. Much as you would in C.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 March 2019 08:42 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/3 "2019-03-21T08:42:58Z")

</div>

> [@qpongoaka](#):
>
> how can I "sense" when a frame is comming

Not sure what you mean by that bit. Each time there is a new value for the subscribed topic in mqtt the MQTT In node will pass on a message, which you can feed into a function node to do your manipulation.  
If you need further help then feed the output of the mqtt node into a debug node and paste what you get here so we can see exactly what the format of the data is.

---

<div class="post-metadata">

**Author:** ![qpongoaka](https://avatars.discourse-cdn.com/v4/letter/q/edb3f5/32.png) [@qpongoaka](https://discourse.nodered.org/u/qpongoaka)\
**Post date:** [22 March 2019 00:21 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/4 "2019-03-22T00:21:17Z")

</div>

Thanks for the reply.  
That's exactly what i want to do, but don't know how to do it in javascript.

Can you possible make an example about it?  
I imagine that it has to be something like:

var number |= (byte1 \<\< 8);  
number |= (byte2);

Thanks again

---

<div class="post-metadata">

**Author:** ![qpongoaka](https://avatars.discourse-cdn.com/v4/letter/q/edb3f5/32.png) [@qpongoaka](https://discourse.nodered.org/u/qpongoaka)\
**Post date:** [22 March 2019 00:29 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/5 "2019-03-22T00:29:26Z")

</div>

That Is what i already achieved

> [@Colin](#):
>
> Each time there is a new value for the subscribed topic in mqtt the MQTT In node will pass on a message, which you can feed into a function node to do your manipulation.

That is what i already achieved. Know what i need to do Is to program the funtion that "understand" that when a (SoF) is coming the next two bytes that comes are the bits of a 16 bits variable.

I don't have a clue of how to shift those bits into a new variable in .js ( and particulary with the payload).

Any code as example about it is more than welcome

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 March 2019 08:14 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/6 "2019-03-22T08:14:11Z")

</div>

Again you have not made it clear what you mean by "understand that when a (SoF) is coming the next two bytes that comes are the bits of a 16 bits variable". Do you mean that you get other types of messages coming in and you need to differentiate them? If so then you can use a Switch node for that. To access the payload in a function node you use msg.payload. Have a look at the nodered docs there are is a lot of useful information there about accessing data in messages and so on.  
As for how to shift data in javascript an internet search for 'javascript bit shift' will take you to sites such as [https://www.w3schools.com/js/js\_bitwise.asp](https://www.w3schools.com/js/js_bitwise.asp)

---

<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 March 2019 09:30 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/7 "2019-03-22T09:30:37Z")

</div>

Operators are well documented...

> **[Bitwise operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_shift_operators)**
>
> Bitwise operators treat their operands as a sequence of 32 bits (zeroes and ones), rather than as decimal, hexadecimal, or octal numbers. For example, the decimal number nine has a binary representation of 1001. Bitwise operators perform their...

So when I said "shift byte1 by 8 then add together" I meant...

`var i = (byte1 << 8) + byte2`  
Alternatively...  
`var i = (byte1 * 256) + byte2`

Of course it depends on endianness which way around you put the bytes.

Then you could use a nodejs buffer...  
[https://nodejs.org/api/buffer.html#buffer\_buf\_readuint16le\_offset](https://nodejs.org/api/buffer.html#buffer_buf_readuint16le_offset)

There are many ways.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 March 2019 09:52 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/8 "2019-03-22T09:52:29Z")

</div>

as it's already coming from MQTT node it may already be a binary buffer... which would make that a good way to do it.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [22 March 2019 10:04 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/9 "2019-03-22T10:04:43Z")

</div>

I don't know if this example may help;

> <https://github.com/Paul-Reed/node-red-contrib-rf-decode/blob/master/rf-decode.js>

---

<div class="post-metadata">

**Author:** ![qpongoaka](https://avatars.discourse-cdn.com/v4/letter/q/edb3f5/32.png) [@qpongoaka](https://discourse.nodered.org/u/qpongoaka)\
**Post date:** [23 March 2019 00:14 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/10 "2019-03-23T00:14:15Z")

</div>

Thanks everyone of you! The information came more than handly!

---

<div class="post-metadata">

**Author:** ![mudmanrv](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@mudmanrv](https://discourse.nodered.org/u/mudmanrv)\
**Post date:** [1 May 2024 16:00 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/11 "2024-05-01T16:00:42Z")

</div>

I'm parsing a 277 byte stream -  
with 2 byte header - 256 byte data 1 byte separator 16 of data and then 2 byte checksum.

Use a function node - and in the code section for that - define a variable - for your beginning or record indicator (BORI) -find it by index of(variable) then, create outputs for BORI+1, BORI +2 etc.

hope that makes sense -here's a chunk of that code-

```auto
// Extract header
let headerStart = msg.payload.indexOf(Buffer.from([0x55, 0xaa]));

// If header found
if(headerStart !== -1) {
    let headerEnd = headerStart + 2;
    // Extract rest of the data according to structure
    let header = msg.payload.slice(headerStart, headerEnd);
    let data1 = msg.payload.slice(headerEnd, headerEnd + 256);
    let spacer = msg.payload.slice(headerEnd + 256, headerEnd + 257);
    let data2 = msg.payload.slice(headerEnd + 257, headerEnd + 257 + 16);
    let checksum = msg.payload.slice(headerEnd + 257 + 16, headerEnd + 257 + 16 + 2);
    let rpm = msg.payload.slice(headerEnd +8, headerEnd + 8+1);
    let mph = msg.payload.slice(headerEnd+6, headerEnd+6+1)
    let map = msg.payload.slice(headerEnd+7, headerEnd+7+1)
   
    // Output the data
    return [{
        topic: "header",
        payload: header
    }, {
        topic: "data1",
        payload: data1
    }, {
        topic: "spacer",
        payload: spacer

```

---

<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:** [1 May 2024 16:02 UTC](https://discourse.nodered.org/t/how-to-form-a-new-number-with-two-bytes/9259/12 "2024-05-01T16:02:35Z")

</div>


