Custom Binary string construction help needed

Good evening all, here is a challenge for the real node-red genuises!!

I am working on an RFID project and need to convert decimal values in a binary string, in a specific order, or change base on a start and end indicator value to create a custom card format.
I have a sample of what the result would be as a sample to work towards.

11101110110000000111000100011000111

inside the above binary string, the structure consists of :

Card Binary data:             --          11101110110000000111000100011000111  ( what I need to recreate )
MSB             :             --          11
Company ID      :   2992      --          101110110000                       [11 (101110110000) 000111000100011000111]
Card ID         :   115811    --          00011100010001100011               [11101110110000 (00011100010001100011) 1]
LSB             :             --          1

here is where I am stuck :
challenge 1) Binary string function - The .toString(2) function works well, except on the card id value when converted to binary seems to miss/ignored the "000" and starts at "1". here is a sample

11100010001100011   from debug 
00011100010001100011 what it suppose to be. 

what is wrong and how do I fix this, I have triple check everything and can't find the issue.

Challenge 2) building the binary string function I use the "combine node" to combine the pre-converted binary data in a single string. it works perfectly in this format ( format 37) and I will get the result if " challenge 1 is sorted. but, I need to create other formats with this same flow if someone can help me figure out how to instruct the "function" where to place the " Facility_id and Card_id subject to the indicated " Start Bit value.

here is my flow (it's clean, no UI and stuff),

[{"id":"25e734b0.ca402c","type":"change","z":"cb870ae2.001588","name":"payload.format_id","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.format_id","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":100,"wires":[["85650f99.daec4"]]},{"id":"e67c8663.e57828","type":"change","z":"cb870ae2.001588","name":"payload.facility_id","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.facility_id","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":280,"wires":[["c829dc10.646b2"]]},{"id":"394e52b0.e94d9e","type":"change","z":"cb870ae2.001588","name":"payload.card_number","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.card_number","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":440,"wires":[["1fc8edc4.ae9632"]]},{"id":"2c2357aa.1b8608","type":"change","z":"cb870ae2.001588","name":"payload.msb","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.msb","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":180,"wires":[["6c47570e.701568"]]},{"id":"84b45524.2e00a8","type":"change","z":"cb870ae2.001588","name":"payload.lsb","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.lsb","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":600,"wires":[["fd8185a0.d60118"]]},{"id":"9b0a0c33.6417d","type":"debug","z":"cb870ae2.001588","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":330,"y":160,"wires":[]},{"id":"6adf4eea.e9b7c","type":"change","z":"cb870ae2.001588","name":"payload.fc_start","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.fc_start","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":360,"wires":[["9da3436.56290c"]]},{"id":"d8f77a9c.5c0358","type":"change","z":"cb870ae2.001588","name":"payload.cn_start","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.cn_start","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":520,"wires":[["839cdc52.8d746"]]},{"id":"85650f99.daec4","type":"debug","z":"cb870ae2.001588","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":100,"wires":[]},{"id":"7b544a65.b58d54","type":"debug","z":"cb870ae2.001588","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":280,"wires":[]},{"id":"9da3436.56290c","type":"debug","z":"cb870ae2.001588","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":360,"wires":[]},{"id":"a34e35a3.dd9728","type":"debug","z":"cb870ae2.001588","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":440,"wires":[]},{"id":"839cdc52.8d746","type":"debug","z":"cb870ae2.001588","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":520,"wires":[]},{"id":"30f3b621.11ab7a","type":"debug","z":"cb870ae2.001588","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":950,"y":180,"wires":[]},{"id":"f72106a7.6d0438","type":"debug","z":"cb870ae2.001588","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":600,"wires":[]},{"id":"143523e1.0759cc","type":"comment","z":"cb870ae2.001588","name":"The payload = Total length of the string in bits = 37 bits / characters ","info":"","x":720,"y":60,"wires":[]},{"id":"6329c53c.bebdbc","type":"comment","z":"cb870ae2.001588","name":"The payload =Data to be converted = Bit 2 – 17 The “A” Field. The binary value of the site / facility code e.g. for a facility code of decimal 16 the bit pattern would be \"0000000000010000\".","info":"","x":1090,"y":240,"wires":[]},{"id":"6c08aabf.6db754","type":"comment","z":"cb870ae2.001588","name":"The payload = the indecation of where in the final string the facility data binary value should start in the string - starts from bits 2/3/4- ","info":"","x":920,"y":320,"wires":[]},{"id":"5932b43a.f2774c","type":"comment","z":"cb870ae2.001588","name":"The payload = indecator of Bit 1 An \"Even\" parity bit that is calculated from bits 2 - 19 inclusive.","info":"","x":800,"y":140,"wires":[]},{"id":"c829dc10.646b2","type":"function","z":"cb870ae2.001588","name":"","func":"msg.payload = msg.payload.toString(2);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":280,"wires":[["7b544a65.b58d54","f4c7dc48.3857c"]]},{"id":"1fc8edc4.ae9632","type":"function","z":"cb870ae2.001588","name":"","func":"msg.payload = msg.payload.toString(2);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":440,"wires":[["a34e35a3.dd9728","f4c7dc48.3857c"]]},{"id":"75e233a7.a2c69c","type":"comment","z":"cb870ae2.001588","name":"The payload =Data to be converted - starting from Bit 18 – 36 The “B” Field. ","info":"","x":740,"y":400,"wires":[]},{"id":"fd2dde07.7125e","type":"comment","z":"cb870ae2.001588","name":"The payload =Bit 18 – 36 The “B” Field. The card number in binary.","info":"","x":720,"y":480,"wires":[]},{"id":"116d5022.5c6db","type":"comment","z":"cb870ae2.001588","name":"The payload = Bit 37 An \"Odd\" parity bit that is calculated from bits 19 - 36 inclusive.","info":"","x":770,"y":560,"wires":[]},{"id":"53335ddb.b48b84","type":"inject","z":"cb870ae2.001588","name":"Card format info and decimal data ","props":[{"p":"payload.format_id","v":"37","vt":"num"},{"p":"payload.msb","v":"3","vt":"num"},{"p":"payload.facility_id","v":"2992","vt":"num"},{"p":"payload.fc_start","v":"2","vt":"num"},{"p":"payload.card_number","v":"115811","vt":"num"},{"p":"payload.cn_start","v":"18","vt":"num"},{"p":"payload.lsb","v":"1","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":220,"y":320,"wires":[["25e734b0.ca402c","2c2357aa.1b8608","e67c8663.e57828","6adf4eea.e9b7c","394e52b0.e94d9e","d8f77a9c.5c0358","84b45524.2e00a8","9b0a0c33.6417d"]]},{"id":"f4c7dc48.3857c","type":"join","z":"cb870ae2.001588","name":"","mode":"custom","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"","joinerType":"str","accumulate":false,"timeout":"","count":"4","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"num","reduceFixup":"","x":1630,"y":360,"wires":[["5a83d29b.c4950c"]]},{"id":"5a83d29b.c4950c","type":"debug","z":"cb870ae2.001588","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1830,"y":360,"wires":[]},{"id":"6c47570e.701568","type":"function","z":"cb870ae2.001588","name":"","func":"msg.payload = msg.payload.toString(2);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":180,"wires":[["30f3b621.11ab7a","f4c7dc48.3857c"]]},{"id":"fd8185a0.d60118","type":"function","z":"cb870ae2.001588","name":"","func":"msg.payload = msg.payload.toString(2);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":600,"wires":[["f72106a7.6d0438","f4c7dc48.3857c"]]}]

I would appreciate your responses, thank you in advance !!

I think you are over complicating it peter.

Based on this...

Card Binary data:             --    11101110110000000111000100011000111  
MSB             :             --    11
Company ID      :   2992      --      101110110000                       
Card ID         :   115811    --                  00011100010001100011   
LSB             :             --                                      1

you can simply use a single function...

var msbStr = msg.payload.msb.toString(2).padStart(2,"0");
var fidStr = msg.payload.facility_id.toString(2).padStart(12,"0");
var cnStr = msg.payload.card_number.toString(2).padStart(20,"0");
var lsbStr = msg.payload.lsb.toString(2)
msg.payload = `${msbStr}${fidStr}${cnStr}${lsbStr}`
return msg;

Demo flow...

[{"id":"431614ce.6da7cc","type":"function","z":"d2945b99.175658","name":"","func":"\nvar msbStr = msg.payload.msb.toString(2).padStart(2,\"0\");\nvar fidStr = msg.payload.facility_id.toString(2).padStart(12,\"0\");\nvar cnStr = msg.payload.card_number.toString(2).padStart(20,\"0\");\nvar lsbStr = msg.payload.lsb.toString(2)\nmsg.payload = `${msbStr}${fidStr}${cnStr}${lsbStr}`\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":590,"y":800,"wires":[["c5f20242.c942d"]]},{"id":"c5f20242.c942d","type":"debug","z":"d2945b99.175658","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":750,"y":800,"wires":[]},{"id":"f19d9f90.66662","type":"inject","z":"d2945b99.175658","name":"Card format info and decimal data ","props":[{"p":"payload.format_id","v":"37","vt":"num"},{"p":"payload.msb","v":"3","vt":"num"},{"p":"payload.facility_id","v":"2992","vt":"num"},{"p":"payload.fc_start","v":"2","vt":"num"},{"p":"payload.card_number","v":"115811","vt":"num"},{"p":"payload.cn_start","v":"18","vt":"num"},{"p":"payload.lsb","v":"1","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":320,"y":800,"wires":[["431614ce.6da7cc"]]}]
1 Like

Hey @Steve-Mcl, thanks man, let me check this and replay swiftly !! :smiley:

Thank you @Steve-Mcl , you're a super star!!!!! I wish I had your Java skills.

Quick question, so if I now stange the starting bits indicators for the values, will they actually move / start from that bits as per the payload ( padStart )?

JavaScript (big difference when you go hunting for tutorials :wink:

Sorry peter, I dont quite understand the question.

Give it a go - see what happens.

If you need more help, try to provide as much detail as possible

no wonder I'm struggling ...f@#$%

yea, Im busy testing . will respond soon

Great Thanks @Steve-Mcl , it works, I just would like to know can this work in reverse, so when I add card binary data/badge card data, to get the value pairs in an object to validate?

then the question of format ID ( 26/27/28..35), which is the structure of the binary string combined out of Card data total number of bits, in our case above was 35 ( 2 bits = MSB ) ( 16 bits = Facility id) (16bits = Card Number) (1 bit = LSB) = Total 35, thus format name = 35

so, will the function move the ( 16 bits = Facility id) and (16bits = Card Number) up and down the bits string subject to the " starting bit value " we allocated in the card format structure?

example: Format 40 = 40 bits ( first 2 bits = MSB ) and ( 16 bits = Facility id) starts on bit 5 and not 3 if the "Faclity id Starting value" = 5 , then move the Facility id binary value up and spaces = 0 ?

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