【问题标题】:How to convert ipv4 address into binary in JavaScript? [closed]如何在 JavaScript 中将 ipv4 地址转换为二进制? [关闭]
【发布时间】:2022-01-26 10:34:28
【问题描述】:

这个简单的输入字段通过单击转换按钮将 ipv4 地址转换为二进制形式,并通过单击重置按钮重置 dom。该字段还具有验证功能,当用户输入错误的 ipv4 地址并点击转换按钮时,该功能会主动工作

【问题讨论】:

标签: javascript arrays converters


【解决方案1】:
<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
      <title>Javascript IPv4 to Binary to Decimal converter</title>
   </head>
   <body >
      <h1>Javascript IPv4 to Binary to Decimal converter</h1>
      <p></p>
      <script type="text/javascript">
         function reset(){  
            document.getElementById("myForm").reset();  
            location.reload(true);
          }   
         
         function ValidateIPaddress(ipaddress) 
         {
         console.log("ipadress",ipaddress)
         
          if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(ipaddress))
           {
           
           
             return (true)
           }
         alert("You have entered an invalid IP address!")
         return (false)
         }
                   
                     function ConvertMe()
                         {
                            
                            
                             var ipbox = document.getElementById("ipbox");
                             
                          
                            
                             
                            
                            
                             var s = ipbox.value;
                             console.log("s",s)
                             var check=ValidateIPaddress(s)
                             if(check==="false")
                             {
                             console.log("invalid adress")
                             }
                             
                             var split = s.split("\.");
                             var binaryResult = "";
                             var decimalResult = "";
                             const array = s.split('.')
                             console.log("hi",array)
         
          let convertedArr = []
          for (var i = 0; i < array.length; i++) {
           let intNumber = parseInt(array[i])
            let binaryNumber = intNumber.toString(2);
            convertedArr.push(binaryNumber)
            console.log('Binary of:' + ' ' +  array[i] + ' ' +'is ' + binaryNumber);
            document.getElementById(`results${i}`).innerHTML = 'Binary of:' + ' ' +  array[i] + ' ' +'is ' + binaryNumber ;
         
         
                            
                             
                         }
                        
                       
                         }
         
                 
      </script>
      <form action="#"  id = "myForm" name="form1">IP:<input id="ipbox"    type="text"><br></br><button onClick="ConvertMe()">Convert</button> <button  onClick = "reset()">Reset</button></br></form>
      <div id="results0"></div>
      <div id="results1"></div>
      <div id="results2"></div>
      <div id="results3"></div>
   </body>
   <br><br></div></body>
   </body>
</html>

【讨论】:

  • false"false" 不同
猜你喜欢
  • 2013-03-10
  • 2013-11-14
  • 2017-11-27
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 2020-11-16
  • 2010-12-06
  • 2013-11-16
相关资源
最近更新 更多