How to convert int to binary with leading zeros in TypeScript

1 Answer

0 votes
const a = 7;
           
console.log("0000".substr(a.toString(2).length) + a.toString(2));
 
const b = 56;
const binary = b.toString(2);
console.log("00000000".substr(binary.length) + binary);
 
  
   
   
   
/*
run:
    
"0111"
"00111000"
    
*/

 



answered Dec 28, 2021 by avibootz

Related questions

1 answer 160 views
1 answer 86 views
1 answer 96 views
1 answer 157 views
1 answer 114 views
1 answer 99 views
1 answer 86 views
...