Ip地址的转换10进制16进制转化//ip转为4个字节的16进制.
var ip="192.111.1.1";
function ipcon(ip){
var ipv = ip;
if(ipv == ''){
alert("获取本机ip出现异常!");
return false;
}else{
var ar = ipv.split(".");
var decm = parseInt(ar[0])*256*256*256+parseInt(ar[1])*256*256+parseInt(ar[2])*256+parseInt(ar[3]);
var iphex = parseInt(decm).toString(16).toUpperCase();
/
/var binary = parseInt(decm).toString(2).toUpperCase();
//ElementById("res").innerHTML = '⼗六进制 = '+hex+'<br> ⼗进制 = '+decm+"<br> ⼆进制 = "+binary;
}
return iphex;
}
ipcon(ip) ;//"C06F0101"
//输⼊16进制ip 转化为10进制ip
function hex(ip){
var ipv = ip;
ipv = UpperCase();
if(ishex(ipv)){
ipv = parseInt(ipv,16)
var b = new Array();
b[0] =0;
b[1] =0;
b[2] =0;
b[3] =0;
var c = 16777216.0;
for (var i = 0; i < 4; i++) {
var k = parseInt(ipv / c);
ipv -= c * k;
b[i]= k;
c = c/256.0;
}
d=b[0]+'.'+b[1]+'.'+b[2]+'.'+b[3];
return false;
}
}hex("C06F0101");//"192.111.1.1"
function decimal(ip){
var ipv =ip;
if ((/[^0-9]/g.test(ipv))  ==false|| ipv != "") {
var b = new Array();
b[0] =0;
b[1] =0;
b[2] =0;
b[3] =0;
var c = 16777216.0;
for (var i = 0; i < 4; i++) {
var k = parseInt(ipv / c);
ipv -= c * k;
b[i]= k;
c = c/256.0;
}
d=b[0]+'.'+b[1]+'.'+b[2]+'.'+b[3];
console.log("ip:"+d);
}else{
return false;
}
}
decimal(2560000001);//ip:152.150.128.1
//判断是否为16进制的数,返回true false
function ishex(num){
var validChar='0123456789ABCDEF';
var flag=true;
var UpperCase();
for(idx=0;idx<x.length;idx++){
if(validChar.indexOf(x.charAt(idx))<0){
return false;
}
}
return true;
}
ishex("a245b");//true
ishex("sa");//false

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。