-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvertip.html
97 lines (97 loc) · 2.61 KB
/
convertip.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!DOCTYPE html>
<html>
<head>
<title>convertip | tools.nex.li</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<h1>convertip</h1>
<p>
Plain: <input id="plain" type="text" value="127.0.0.1" autofocus><br>
Binary: <input id="binary" type="text" value="01111111000000000000000000000001"><br>
Decimal: <input id="decimal" type="text" value="2130706433">
</p>
<p>Copyright (c) 2017-2019, <a href="https://hell.sh/" target="_blank">Hell.sh</a> · <a href="https://hell.sh/privacy" target="_blank">Privacy Policy</a></p>
<script>
document.getElementById("plain").oninput=function(event)
{
var arr=document.getElementById("plain").value.split(".");
if(arr.length!=4)
{
document.getElementById("decimal").value="[invalid ip]";
return;
}
var bin="";
for(var i=0;i<4;i++)
{
if(arr[i]<0||arr[i]>255)
{
document.getElementById("decimal").value="[invalid ip]";
return;
}
bin+=dec2bin(arr[i],128);
}
document.getElementById("binary").value=bin;
document.getElementById("decimal").value=bin2dec(bin);
};
document.getElementById("binary").oninput=function(event)
{
var bin=document.getElementById("binary").value;
if(bin.length!=32)
{
document.getElementById("plain").value="[invalid ip]";
document.getElementById("decimal").value="[invalid ip]";
return;
}
document.getElementById("plain").value=bin2dec(bin.substr(0,8))+"."+bin2dec(bin.substr(8,8))+"."+bin2dec(bin.substr(16,8))+"."+bin2dec(bin.substr(24,8));
document.getElementById("decimal").value=bin2dec(bin);
};
document.getElementById("decimal").oninput=function(event)
{
var dec=document.getElementById("decimal").value;
if(isNaN(dec)||dec<0||dec>4294967295)
{
document.getElementById("plain").value="[invalid ip]";
document.getElementById("binary").value="[invalid ip]";
return;
}
var bin=dec2bin(dec,2147483648);
document.getElementById("binary").value=bin;
document.getElementById("plain").value=bin2dec(bin.substr(0,8))+"."+bin2dec(bin.substr(8,8))+"."+bin2dec(bin.substr(16,8))+"."+bin2dec(bin.substr(24,8));
};
function dec2bin(dec,limit)
{
var bin="",m=limit;
do
{
if(dec>=m)
{
dec-=m;
bin+="1";
}
else
{
bin+="0";
}
m/=2;
}
while(m>=1);
return bin;
}
function bin2dec(bin)
{
bin=bin.split("").reverse();
var m=1,dec=0;
for(var i in bin)
{
if(bin[i]=="1")
{
dec+=m;
}
m*=2;
}
return dec;
}
</script>
</body>
</html>