How to convert binary to hex with leading zeros in TypeScript

1 Answer

0 votes
let b = '11010';
let hex = parseInt(b, 2).toString(16).toUpperCase();
hex = hex.padStart(4, "0")
console.log(hex);
  
  
b = '1001101011';
hex = parseInt(b, 2).toString(16).toUpperCase();
hex = hex.padStart(4, "0")
console.log(hex);
  
  
     
    
    
/*
run:
     
"001A"
"026B"
     
*/

 



answered Dec 28, 2021 by avibootz
...