rajuahammad73

IP Address to Binary Conversion by JavaScript

Oct 19th, 2020 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fullip = "172.16.20.51/24";
  2. const ip = fullip.split('/')[0];
  3. const subnet = fullip.split('/')[1];
  4. const ipslipt = ip.split('.');
  5. const binaryIp = [];
  6. const netAdreess = Math.pow(2, subnet);
  7. const hostAdress = Math.pow(2, (32-subnet));
  8.  
  9. ipslipt.map(item=>{
  10.   let singleIp = parseInt(item).toString(2);
  11.   if(singleIp.length < 8){
  12.     let shortestIp = 8 - (singleIp.length);
  13.     for(let i = 1; i <= shortestIp;i++){
  14.       singleIp = 0 + singleIp;
  15.     }
  16.   }
  17.   binaryIp.push(singleIp);
  18. });
  19.  
  20. console.log("Binary IP: " + binaryIp.join('.'));
  21. console.log("Network Address: " + netAdreess);
  22. console.log("Host Address: " + hostAdress);
Add Comment
Please, Sign In to add comment