I have decimal number, need to convert to binary and have a checkbox linked to every bit of that binary number

StackOverflow https://stackoverflow.com/questions/16196366

  •  11-04-2022
  •  | 
  •  

Question

This is hard to explain but I'll do my best.

I'm using a PLC with webserver and some javascript code I wrote to write values from the registers of the PLC to your webpage.

1 of the functions a PLC has is a time schedule (you know, you can select the days and hours a light must go on for example). To set the schedule I've created some input boxes for the time and 7 checkboxes for each day of the week. You have to send your value in decimal to the controller and that's no problem for the hours and minutes.

However, the weekdays are represented by a binary number, but I need to write it in decimal to the controller.

So I have 7 checkboxes, each representing a binary digit (so Monday is 1, Tuesday is 2, Wednesday is 4, Thursday is 8, and so on) so when I want to create a decimal number out of it, it's easy, just sum up which checkboxes are checked, got that done.

But now I need it the other way around, this time I get a decimal number (representing which days are set) from the controller and I need to represent this on the checkboxes, so probably convert the decimal to binary but then how do I link the checkboxes to an individual digit?

Hope somebody can understand this and help me.

<input type="checkbox" id="mon"></td>
<input type="checkbox" id="tue"></td>
<input type="checkbox" id="wed"></td>
<input type="checkbox" id="thu"></td>
<input type="checkbox" id="fri"></td>
<input type="checkbox" id="sat"></td>
<input type="checkbox" id="sun"></td>

These are my checkboxes and this is some of my javascript:

var Weekday = 0;
if($('#mon').is(':checked')){ Weekday += 1 }  //this is code to create the decimal number 
if($('#tue').is(':checked')){ Weekday += 2 }
if($('#wed').is(':checked')){ Weekday += 4 }
if($('#thu').is(':checked')){ Weekday += 8 }
if($('#fri').is(':checked')){ Weekday += 16 }
if($('#sat').is(':checked')){ Weekday += 32 }
if($('#sun').is(':checked')){ Weekday += 64 }

How do I do this the other way around? I get a decimal number, need to convert to binary and the with javascript check the right checkboxes according to which bits are set.

Any suggestions?

Was it helpful?

Solution

You can use bitwise operators.

For example.

var number = 54;
if (number & 1) { $('#mon').attr('checked', 'checked'); }

if (number & 2) { $('#tue').attr('checked', 'checked'); }

if (number & 4) { $('#wed').attr('checked', 'checked'); }

if (number & 8) { $('#thu').attr('checked', 'checked'); }

if (number & 16) { $('#fri').attr('checked', 'checked'); }

etc...

OTHER TIPS

Binary decomposition of a number :

 function getOnesAndZeroesFromInt(weekdays){
     var tab = [];
     for(var i=0; i<7; i++){
         tab[i] = weekdays % 2;
         weekdays /=  2;
     }
     return tab
 }

Another suggestion (fiddle) - merely putting chead23's answer in a loop :

HTML :

<input type="checkbox" id="mon" data-weight=1></td>
<input type="checkbox" id="tue" data-weight=2></td>
<input type="checkbox" id="wed" data-weight=4></td>
<input type="checkbox" id="thu" data-weight=8></td>
<input type="checkbox" id="fri" data-weight=16></td>
<input type="checkbox" id="sat" data-weight=32></td>
<input type="checkbox" id="sun" data-weight=64></td>

Javascript :

function getWeekDays() {
    var weekdays = 0;
    $('#table :checked').each(function () {
        weekdays += parseInt($(this).attr('data-weight'), 10);
    });
    return weekdays;
}

function setWeekDays(weekdays) {
    $('#table input[type="checkbox"]').each(function () {
        var i = this.getAttribute('data-weight');
        $(this).prop('checked', weekdays & i);
    });
}
let yourNumber = 343234729;
let decomposed = yourNumber.toString(2).split('').reverse().map((bit, index) => bit === '1' ? 1 << index : 0).filter(x => x > 0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top