JSFiddle - React, Tailwind, and code Playground
by sundaramkumar
HTML
Input: <textarea id="ipRange" rows="5" cols="50"></textarea>
<button onclick="checkHosts()">Check</button><br/>
Ouput: <textarea id="output" rows="5" cols="50"></textarea>
<!--
200::1-200::4
2001:db8:abc:1400:: - 2001:db8:abc:1400:ffff:ffff:ffff:ffff
2001:db8:: - 2001:db8::ffff:ffff:ffff:ffff
2001:db8:0:0:0:0:0:0 - 2001:db8:0:0:ffff:ffff:ffff:ffff
-->
JavaScript
/***
* expand the ipv6 in proper format
* convert each hex digit to decimal
* Reg: https://github.com/peterolson/BigInteger.js
*
***/
// IPv6 address
var networkAddress = new Array(0,0,0,0,0,0,0,0),
ADDRESS_BLOCKS = new Array();
var TOTAL_BITS = 128,
MASK_BITS = 0,
NETWORK_BITS = 0,
DEFAULT_NETWORK_BITS = 8,
TOTAL_NETWORK_BITS = 64,
SUBNET_BITS = 0,
HOST_BITS = 64,
TOTAL_HOST_BITS = 64,
WORD = 16,
TOTAL_WORD_COUNT = 8,
TOTAL_COLON_COUNT = 7,
HEX_BLOCK_DIGITS = 4,
DEFAULT_ADDRESS_BLOCK = 'block1', //No I18N
SUBNET_LIST_START_INDEX = 0,
SUBNET_LIST_END_INDEX = 16,
MAX_SIZE_OF_SUBNET = 1024;
function checkHosts(){
var ipAry = document.getElementById("ipRange").value.split("-");
number1=checkNumberOfIps(ipAry[0].trim());
number2=checkNumberOfIps(ipAry[1].trim());
console.debug("No Of IPs ", bigInt(number2).minus(number1) )
document.getElementById("output").value = bigInt(number2).minus(number1) ;
}
function checkNumberOfIps(val){
var ip = val
var hexArray= formatHexAddress(ip);
var decAry=[];
for(var i=0;i<hexArray.length;i++){
octAry = hexArray[i].split("");
for(var j=0;j<octAry.length;j++){
dec = parseInt(octAry[j],16)+ " ";
decAry.push(dec)
}
}
var total=bigInt();
var tmpTotal = 0;
var curPower=0;
for(var pos=decAry.length-1;pos>=0;pos--){
total = bigInt(total).plus(decAry[pos]*Math.pow(16,curPower));
//total = Number(total) + Number( decAry[pos]*Math.pow(16,curPower) )
curPower++;
}
// document.getElementById("output").value += Number(total)
// document.getElementById("output").value +="\n";
return total;
}
//Converts IPv6 in the form 'fd30::1:ff4e:3e:9:e' to 'fd30:0000:0000:0001:ff4e:003e:0009:000e'
function formatHexAddress(rawInputAddress){
//rawInputAddress = 'fe80::';
//rawInputAddress = 'FE80:0000:0000:0000:0202:B3FF:FE1E:8329';
//rawInputAddress = 'fdf4:af5d:8d13:7664::';
//console.log('RawInputAddress : '+rawInputAddress+', Type : '+typeof(rawInputAddress));
var toReturnArray = new...