Convert Bits to Bytes in Node-Red
I'm new to JavaScript and I use Node-Red to read an write from a Database.
I receive from the database an object that contains the status of 8 digital inputs.
Each inputs is represented as a bit.
I'm looking for a method to combine each bits into a byte.
This is the object that I receive from the database:
array[1]
0: object
idx: 10
ts: "2018-11-21T06:12:45.000Z"
in_0: 1
in_1: 1
in_2: 1
in_3: 1
in_4: 1
in_5: 1
in_6: 1
in_7: 1
in_x represent the input position.
As out I would like to receive a byte that represent the combination of each single byte.
For example:
in0: 0,
in1: 1,
in2: 0,
in3: 0,
in4: 0,
in5: 1,
in6: 0,
in7: 0,
The output byte will be: 00100001 in binary that converted to byte is 33
Any suggestions?
Thanks in advance.
javascript bit-manipulation node-red
add a comment |
I'm new to JavaScript and I use Node-Red to read an write from a Database.
I receive from the database an object that contains the status of 8 digital inputs.
Each inputs is represented as a bit.
I'm looking for a method to combine each bits into a byte.
This is the object that I receive from the database:
array[1]
0: object
idx: 10
ts: "2018-11-21T06:12:45.000Z"
in_0: 1
in_1: 1
in_2: 1
in_3: 1
in_4: 1
in_5: 1
in_6: 1
in_7: 1
in_x represent the input position.
As out I would like to receive a byte that represent the combination of each single byte.
For example:
in0: 0,
in1: 1,
in2: 0,
in3: 0,
in4: 0,
in5: 1,
in6: 0,
in7: 0,
The output byte will be: 00100001 in binary that converted to byte is 33
Any suggestions?
Thanks in advance.
javascript bit-manipulation node-red
Will you please explain a bit, what exactly you are expecting in output.
– Anki
Nov 21 '18 at 8:55
What have you already tried? Also what representation is the data in LSB, MSB, 2's complement binary?
– hardillb
Nov 21 '18 at 8:57
add a comment |
I'm new to JavaScript and I use Node-Red to read an write from a Database.
I receive from the database an object that contains the status of 8 digital inputs.
Each inputs is represented as a bit.
I'm looking for a method to combine each bits into a byte.
This is the object that I receive from the database:
array[1]
0: object
idx: 10
ts: "2018-11-21T06:12:45.000Z"
in_0: 1
in_1: 1
in_2: 1
in_3: 1
in_4: 1
in_5: 1
in_6: 1
in_7: 1
in_x represent the input position.
As out I would like to receive a byte that represent the combination of each single byte.
For example:
in0: 0,
in1: 1,
in2: 0,
in3: 0,
in4: 0,
in5: 1,
in6: 0,
in7: 0,
The output byte will be: 00100001 in binary that converted to byte is 33
Any suggestions?
Thanks in advance.
javascript bit-manipulation node-red
I'm new to JavaScript and I use Node-Red to read an write from a Database.
I receive from the database an object that contains the status of 8 digital inputs.
Each inputs is represented as a bit.
I'm looking for a method to combine each bits into a byte.
This is the object that I receive from the database:
array[1]
0: object
idx: 10
ts: "2018-11-21T06:12:45.000Z"
in_0: 1
in_1: 1
in_2: 1
in_3: 1
in_4: 1
in_5: 1
in_6: 1
in_7: 1
in_x represent the input position.
As out I would like to receive a byte that represent the combination of each single byte.
For example:
in0: 0,
in1: 1,
in2: 0,
in3: 0,
in4: 0,
in5: 1,
in6: 0,
in7: 0,
The output byte will be: 00100001 in binary that converted to byte is 33
Any suggestions?
Thanks in advance.
javascript bit-manipulation node-red
javascript bit-manipulation node-red
edited Nov 21 '18 at 11:05
Federico
asked Nov 21 '18 at 8:52
FedericoFederico
3141519
3141519
Will you please explain a bit, what exactly you are expecting in output.
– Anki
Nov 21 '18 at 8:55
What have you already tried? Also what representation is the data in LSB, MSB, 2's complement binary?
– hardillb
Nov 21 '18 at 8:57
add a comment |
Will you please explain a bit, what exactly you are expecting in output.
– Anki
Nov 21 '18 at 8:55
What have you already tried? Also what representation is the data in LSB, MSB, 2's complement binary?
– hardillb
Nov 21 '18 at 8:57
Will you please explain a bit, what exactly you are expecting in output.
– Anki
Nov 21 '18 at 8:55
Will you please explain a bit, what exactly you are expecting in output.
– Anki
Nov 21 '18 at 8:55
What have you already tried? Also what representation is the data in LSB, MSB, 2's complement binary?
– hardillb
Nov 21 '18 at 8:57
What have you already tried? Also what representation is the data in LSB, MSB, 2's complement binary?
– hardillb
Nov 21 '18 at 8:57
add a comment |
1 Answer
1
active
oldest
votes
The following code works as you requested*:
var output =
arr[0].in_0 +
(arr[0].in_1 << 1) +
(arr[0].in_2 << 2) +
(arr[0].in_3 << 3) +
(arr[0].in_4 << 4) +
(arr[0].in_5 << 5) +
(arr[0].in_6 << 6) +
(arr[0].in_7 << 7);
This code assumes that each variable can only be a 1 or a 0. Anything else will result in nonsense.
I have used the Left Bit Shift operator (<<) to obtain the power of two for each on bit.
You have specified that in_7 is the Most Significant Bit. If it is actually the Least Significant Bit, reverse the order of the in_x variables.
*The result is not a byte, but it does contain the number that I think you're expecting.
Thanks for the reply but as output I need a byte obtained with Bit Shift. This because the byte will be sent to a Shift Register in order to enable relays.
– Federico
Nov 21 '18 at 10:46
Can you provide any more details on the requirements of whatever system you're trying to enter the data into? Perhaps it expects input in a Uint8 Array?
– Gary Ott
Nov 21 '18 at 11:16
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Gary Ott
Nov 21 '18 at 11:16
The byte obtained will be sent to a PLC via node-red. So I need an integer.
– Federico
Nov 21 '18 at 12:06
1
I'm sorry I misinterpreted the answer. It's works! Thank you very much
– Federico
Nov 21 '18 at 13:59
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408303%2fconvert-bits-to-bytes-in-node-red%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The following code works as you requested*:
var output =
arr[0].in_0 +
(arr[0].in_1 << 1) +
(arr[0].in_2 << 2) +
(arr[0].in_3 << 3) +
(arr[0].in_4 << 4) +
(arr[0].in_5 << 5) +
(arr[0].in_6 << 6) +
(arr[0].in_7 << 7);
This code assumes that each variable can only be a 1 or a 0. Anything else will result in nonsense.
I have used the Left Bit Shift operator (<<) to obtain the power of two for each on bit.
You have specified that in_7 is the Most Significant Bit. If it is actually the Least Significant Bit, reverse the order of the in_x variables.
*The result is not a byte, but it does contain the number that I think you're expecting.
Thanks for the reply but as output I need a byte obtained with Bit Shift. This because the byte will be sent to a Shift Register in order to enable relays.
– Federico
Nov 21 '18 at 10:46
Can you provide any more details on the requirements of whatever system you're trying to enter the data into? Perhaps it expects input in a Uint8 Array?
– Gary Ott
Nov 21 '18 at 11:16
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Gary Ott
Nov 21 '18 at 11:16
The byte obtained will be sent to a PLC via node-red. So I need an integer.
– Federico
Nov 21 '18 at 12:06
1
I'm sorry I misinterpreted the answer. It's works! Thank you very much
– Federico
Nov 21 '18 at 13:59
|
show 2 more comments
The following code works as you requested*:
var output =
arr[0].in_0 +
(arr[0].in_1 << 1) +
(arr[0].in_2 << 2) +
(arr[0].in_3 << 3) +
(arr[0].in_4 << 4) +
(arr[0].in_5 << 5) +
(arr[0].in_6 << 6) +
(arr[0].in_7 << 7);
This code assumes that each variable can only be a 1 or a 0. Anything else will result in nonsense.
I have used the Left Bit Shift operator (<<) to obtain the power of two for each on bit.
You have specified that in_7 is the Most Significant Bit. If it is actually the Least Significant Bit, reverse the order of the in_x variables.
*The result is not a byte, but it does contain the number that I think you're expecting.
Thanks for the reply but as output I need a byte obtained with Bit Shift. This because the byte will be sent to a Shift Register in order to enable relays.
– Federico
Nov 21 '18 at 10:46
Can you provide any more details on the requirements of whatever system you're trying to enter the data into? Perhaps it expects input in a Uint8 Array?
– Gary Ott
Nov 21 '18 at 11:16
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Gary Ott
Nov 21 '18 at 11:16
The byte obtained will be sent to a PLC via node-red. So I need an integer.
– Federico
Nov 21 '18 at 12:06
1
I'm sorry I misinterpreted the answer. It's works! Thank you very much
– Federico
Nov 21 '18 at 13:59
|
show 2 more comments
The following code works as you requested*:
var output =
arr[0].in_0 +
(arr[0].in_1 << 1) +
(arr[0].in_2 << 2) +
(arr[0].in_3 << 3) +
(arr[0].in_4 << 4) +
(arr[0].in_5 << 5) +
(arr[0].in_6 << 6) +
(arr[0].in_7 << 7);
This code assumes that each variable can only be a 1 or a 0. Anything else will result in nonsense.
I have used the Left Bit Shift operator (<<) to obtain the power of two for each on bit.
You have specified that in_7 is the Most Significant Bit. If it is actually the Least Significant Bit, reverse the order of the in_x variables.
*The result is not a byte, but it does contain the number that I think you're expecting.
The following code works as you requested*:
var output =
arr[0].in_0 +
(arr[0].in_1 << 1) +
(arr[0].in_2 << 2) +
(arr[0].in_3 << 3) +
(arr[0].in_4 << 4) +
(arr[0].in_5 << 5) +
(arr[0].in_6 << 6) +
(arr[0].in_7 << 7);
This code assumes that each variable can only be a 1 or a 0. Anything else will result in nonsense.
I have used the Left Bit Shift operator (<<) to obtain the power of two for each on bit.
You have specified that in_7 is the Most Significant Bit. If it is actually the Least Significant Bit, reverse the order of the in_x variables.
*The result is not a byte, but it does contain the number that I think you're expecting.
edited Nov 21 '18 at 9:41
answered Nov 21 '18 at 9:34
Gary OttGary Ott
20128
20128
Thanks for the reply but as output I need a byte obtained with Bit Shift. This because the byte will be sent to a Shift Register in order to enable relays.
– Federico
Nov 21 '18 at 10:46
Can you provide any more details on the requirements of whatever system you're trying to enter the data into? Perhaps it expects input in a Uint8 Array?
– Gary Ott
Nov 21 '18 at 11:16
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Gary Ott
Nov 21 '18 at 11:16
The byte obtained will be sent to a PLC via node-red. So I need an integer.
– Federico
Nov 21 '18 at 12:06
1
I'm sorry I misinterpreted the answer. It's works! Thank you very much
– Federico
Nov 21 '18 at 13:59
|
show 2 more comments
Thanks for the reply but as output I need a byte obtained with Bit Shift. This because the byte will be sent to a Shift Register in order to enable relays.
– Federico
Nov 21 '18 at 10:46
Can you provide any more details on the requirements of whatever system you're trying to enter the data into? Perhaps it expects input in a Uint8 Array?
– Gary Ott
Nov 21 '18 at 11:16
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Gary Ott
Nov 21 '18 at 11:16
The byte obtained will be sent to a PLC via node-red. So I need an integer.
– Federico
Nov 21 '18 at 12:06
1
I'm sorry I misinterpreted the answer. It's works! Thank you very much
– Federico
Nov 21 '18 at 13:59
Thanks for the reply but as output I need a byte obtained with Bit Shift. This because the byte will be sent to a Shift Register in order to enable relays.
– Federico
Nov 21 '18 at 10:46
Thanks for the reply but as output I need a byte obtained with Bit Shift. This because the byte will be sent to a Shift Register in order to enable relays.
– Federico
Nov 21 '18 at 10:46
Can you provide any more details on the requirements of whatever system you're trying to enter the data into? Perhaps it expects input in a Uint8 Array?
– Gary Ott
Nov 21 '18 at 11:16
Can you provide any more details on the requirements of whatever system you're trying to enter the data into? Perhaps it expects input in a Uint8 Array?
– Gary Ott
Nov 21 '18 at 11:16
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Gary Ott
Nov 21 '18 at 11:16
See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– Gary Ott
Nov 21 '18 at 11:16
The byte obtained will be sent to a PLC via node-red. So I need an integer.
– Federico
Nov 21 '18 at 12:06
The byte obtained will be sent to a PLC via node-red. So I need an integer.
– Federico
Nov 21 '18 at 12:06
1
1
I'm sorry I misinterpreted the answer. It's works! Thank you very much
– Federico
Nov 21 '18 at 13:59
I'm sorry I misinterpreted the answer. It's works! Thank you very much
– Federico
Nov 21 '18 at 13:59
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408303%2fconvert-bits-to-bytes-in-node-red%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Will you please explain a bit, what exactly you are expecting in output.
– Anki
Nov 21 '18 at 8:55
What have you already tried? Also what representation is the data in LSB, MSB, 2's complement binary?
– hardillb
Nov 21 '18 at 8:57