-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.js
More file actions
50 lines (48 loc) · 1.3 KB
/
converter.js
File metadata and controls
50 lines (48 loc) · 1.3 KB
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
console.log("converter.js loaded");
var AddressToBinary = function(address, bitRate)
{
if (!(ipaddr.isValid(address))) return false;
if (bitRate == 32)
{
let byteArr = ipaddr.IPv4.parser(address);
let binStr = "";
for (let i = 0; i < byteArr.length; i++)
{
let currByte = byteArr[i].toString(2);
while (currByte.length < 8)
{
currByte = "0" + currByte;
}
binStr = binStr + currByte;
}
return binStr;
}
else if (bitRate == 128)
{
let byteArr = ipaddr.IPv6.parser(address).parts;
let binStr = "";
for (let i = 0; i < byteArr.length; i++)
{
let currByte = byteArr[i].toString(2);
while (currByte.length < 16)
{
currByte = "0" + currByte;
}
binStr = binStr + currByte;
}
return binStr;
}
}
var BinaryToAddress = function(binStr, bitRate)
{
while (binStr.length < bitRate)
{
binStr = "0" + binStr;
}
let byteArr = [];
for (let i = 0; i < binStr.length; i += 8)
{
byteArr.push(parseInt(binStr.substr(i, 8), 2));
}
return ipaddr.fromByteArray(byteArr).toString();
}