...

Calculate IP Length

Leos
07-11-2011, 02:01 PM
Hello.

I wonder is there any javascript to calculate IP length

For example I have

10.52.28.0

VLAN_LAST which is

10.52.29.254

As I know:result is 512

or another example:

10.52.64.11

VLAN_LAST which is

10.52.64.15

As I know:result is 5

How to count it in javascript code ?
Results are "numbers"

Best regards

Leos.

Kor
07-11-2011, 02:04 PM
JavaScript, as a client-side language, is not able to capture the IP (which is a server-side job). Thus, your question become without any basis within the JavaScript Forum. You should use a server-side language. Which is your case: PHP? ASP.NET, Java, Pearl? Other?

Or maybe you confounded the Forum. This is the JavaScript Forum, have you noticed?

Leos
07-11-2011, 02:16 PM
Yes and I need support in javascript.
I don't need to capture IP.. I need help in this kind of math javascript calculation.

I need calculation in javascript. Subtraction 2 values of IP.

10.52.64.11 and 10.52.64.15

something like 11 12 13 14 15 so answer is 5

10.52.28.0 and 10.52.29.254

something like 256 + 254+2 = 512

I'm not sure how to count this value of IP LENGHT

Are you able to help me?

For example if I have 2 IP's I need to substract them and count LENGHT as in example above

Best regards :)

Kor
07-11-2011, 03:25 PM
So, you have several string variables defined in JavaScript? Like "10.52.28.0", "10.52.29.254"...?

Could be something like this:

function countIP(startIP,endIP){
var d1=Number(endIP.split('.')[2])-Number(startIP.split('.')[2]);
var d2=Number(endIP.split('.')[3])-Number(startIP.split('.')[3])+1;
var n=d1*256+d1+d2;
return n;
}

Leos
07-12-2011, 01:50 PM
Hello

Thanks for suggestions.
I appreciate this.

I have error that n is = NaN

it looks like it doesn't recognize "numbers"

hmm

devnull69
07-12-2011, 02:26 PM
There must be a typo in your code. The function works fine although it has a minor flaw

function countIP(startIP,endIP){
var d1=Number(endIP.split('.')[2])-Number(startIP.split('.')[2]);
var d2=Number(endIP.split('.')[3])-Number(startIP.split('.')[3])+1;
var n=d1*256+d2;
return n;
}

This will give "511" which is correct (your example above wasn't correct here)

Krupski
07-12-2011, 09:58 PM
There must be a typo in your code. The function works fine although it has a minor flaw

I would use a Regex to pull out the four octet numbers. You can't guarantee how many digits will be in each one... but the "." separator between them is reliable.

devnull69
07-12-2011, 10:27 PM
That's what the split('.') is for ... the number of digits is not relevant there. Number() will turn them into a numeric type anyway

Old Pedant
07-13-2011, 12:26 AM
As long as you are going to compare the last two parts of the IP address, why not compare the whole thing??

<script type="text/javascript">
function ipToInt( ip )
{
var parts = ip.split(".");
var val = 0;
for ( var p = 0; p < 4; ++p )
{
val = ( val * 256 + parseInt( parts[p] ) );
}
return val;
}
function ipDiff( ip1, ip2 )
{
return Math.abs( ipToInt( ip1 ) - ipToInt( ip2 ) ) + 1;
}
</script>

No? Using Math.abs() means it doesn't matter which order you give the ip addresses in.

And, yes, there are a few companies that have ranges that need to use 3 parts of the IP address (don't know of any needing 4 parts). Google is one.

Leos
07-14-2011, 10:37 AM
I'm investigatin your proposition in my soft.
I'll give info does it works or not ;)
THANKS !

Kor
07-14-2011, 07:39 PM
Hello

Thanks for suggestions.
I appreciate this.

I have error that n is = NaN

it looks like it doesn't recognize "numbers"

hmm
I don't think so:

<script type="text/javascript">
function countIP(startIP,endIP){
var d1=Number(endIP.split('.')[2])-Number(startIP.split('.')[2]);
var d2=Number(endIP.split('.')[3])-Number(startIP.split('.')[3])+1;
var n=d1*256+d1+d2;
return n;
}
var nr=countIP('10.52.28.0','10.52.29.254');
</script>

But make sure that your IPs are formatted as strings. After all, a value like 10.52.28.0 has no meaning as a number, because you can not have multiple floated dots as delimiters.

Maybe you have not sense that in JavaScript there is a firm difference between strings and numbers.

This is a number

var x1=10.52;

this is a string:

var x2='10.52.28.0';
//or
var x3="10.52.28.0";

Strings are quoted. Numbers are not.

Leos
08-02-2011, 09:44 AM
As long as you are going to compare the last two parts of the IP address, why not compare the whole thing??

[code]
<script type="text/javascript">
function ipToInt( ip )
{
var parts = ip.split(".");
var val = 0;
for ( var p = 0; p < 4; ++p )
{
val = ( val * 256 + parseInt( parts[p] ) );
}
return val;
}

what ip is it ?

Leos
08-18-2011, 12:01 PM
There must be a typo in your code. The function works fine although it has a minor flaw

function countIP(startIP,endIP){
var d1=Number(endIP.split('.')[2])-Number(startIP.split('.')[2]);
var d2=Number(endIP.split('.')[3])-Number(startIP.split('.')[3])+1;
var n=d1*256+d2;
return n;
}

This will give "511" which is correct (your example above wasn't correct here)

is it possible to to the same but with "SUBSTRING" method ? I would like to remove .split function because it doesn't work in my software.

my script is working when ipt is for example 10.52.00.4 and 10.52.00.15

a = Number(ip2.substring(6,8))-Number(ip1.substring(6,8));
scriptletContext.put('a',a);
b = Number(ip2.substring(9))-Number(ip1.substring(9));
scriptletContext.put('b',b);
n=a*253+a+b+1;

but when the ip's are 10.52.0.4 and 10.52.0.15

it's not taking good numbers.

and split function doesn't work for me in my soft ;/

Old Pedant
08-18-2011, 10:27 PM
What "soft" are you using???

I don't know of ANY browser that doesn't support split()

It's been around since at least 1998, and I think before.

Leos
08-19-2011, 07:18 AM
Pm Sent.

The issue is to remove this stupid dots and do the easy count. But I wonder how to do this for example with substring method...

Old Pedant
08-19-2011, 07:45 AM
You can do it with substring, but it's a pain in the neck.

var ip = "189.1.32.221";

var parts = [];
var temp = ip;
for ( var p = 0; p < 3; ++p )
{
var dotat = temp.indexOf(".");
parts[p] = temp.substring(0,dotat);
temp = temp.substring(dotat+1);
}
parts[3] = temp;

Compare the to doing
var ip = "189.1.32.221";

var parts = ip.split(".");

Yes, the contents of the array parts is identical in the two cases.

Old Pedant
08-19-2011, 07:54 AM
And yes, I read you are using Rhino Javascript (why?), but I'd bet a dollar or two that Rhino *does* have Array.split(). After all, it even has some JavaScript 1.7 features.

And you say you want to "do the easy count":

var ip = "189.1.32.221";

var count = ip.split(".").length;

venegal
08-19-2011, 05:27 PM
If you really don't have the .split method available, please don't immediately resort to some ugly substring parsing — there are other methods that work just as well.

For instance
var parts = ip.match(/[^.]+/g);
does exactly (or, at least, sufficiently) the same as
var parts = ip.split('.');

venegal
08-19-2011, 06:22 PM
And here's an ipToInt one-liner that doesn't use .split and works for whole IP addresses (leveraging Javascript's ability to parse hex strings):

function ipToInt (ip) {
return parseInt(ip.replace(/\d+\.?/g, function (m) {return (Number(m) > 15 ? '' : 0) + Number(m).toString(16);}), 16);
}

Old Pedant
08-19-2011, 08:53 PM
Have to wonder, though, if Rhino Javascript can't do a simple split() then can it do either of the functions you posted, Venegal. After reading up a little on Rhino Javascript, I would *think* it could do split() but on the other hand it's non-standard in several other ways, so who knows?

Leos
08-22-2011, 01:37 PM
You can do it with substring, but it's a pain in the neck.

var ip = "189.1.32.221";

var parts = [];
var temp = ip;
for ( var p = 0; p < 3; ++p )
{
var dotat = temp.indexOf(".");
parts[p] = temp.substring(0,dotat);
temp = temp.substring(dotat+1);
}
parts[3] = temp;

Compare the to doing
var ip = "189.1.32.221";

var parts = ip.split(".");

Yes, the contents of the array parts is identical in the two cases.

THANKS FOR HELP. I'm SENDING much appreciation for helping in this case. mr. Old Pedant YOU ARE GREAT !

this is how my calculation is:

var ipa = 10.52.0.23;
var ipb = 10.52.0.42;

var partsa = [];
var partsb = [];
var tempa = ipa;
var tempb = ipb;

for ( var p = 0; p < 3; ++p )
{
var dot1t = tempa.indexOf(".");
var dot2t = tempb.indexOf(".");
partsa[p] = tempa.substring(0,dot1t);
partsb[p] = tempb.substring(0,dot2t);
tempa = tempa.substring(dot1t+1);
tempb = tempb.substring(dot2t+1);
}
partsa[3] = tempa;
partsb[3] = tempb;

var CAALI = ((parts2[0]-parts1[0])*16387064)+((parts2[1]-parts1[1])*64516)+((parts2[2]-parts1[2])*254)+((parts2[3]-parts1[3])*1)+1);

Philip M
08-22-2011, 01:59 PM
That cannot possibly work. :(

var ipa = 10.52.0.23; // a string so in quotes
var ipb = 10.52.0.42; // a string so in quotes

var CAALI = ((parts2[0]-parts1[0])*16387064); // parts2 and parts1 are not defined

Leos
08-22-2011, 02:09 PM
That cannot possibly work. :(

var ipa = 10.52.0.23; // a string so in quotes
var ipb = 10.52.0.42; // a string so in quotes

var CAALI = ((parts2[0]-parts1[0])*16387064); // parts2 and parts1 are not defined

thanks ;) it was pasted to fast ;)

var ipa = "10.52.0.23";
var ipb = "10.52.0.42";
var CAALI = ((partsb[0]-partsa[0])*16387064);

siberia-man
08-22-2011, 04:24 PM
The issue is to remove this stupid dots and do the easy count. But I wonder how to do this for example with substring method...

var ip = '127.0.0.1';

// convert the dot-notation
// to the hex image '7f000001'
var hex = ip.replace(/(?:^|\.)(\d+)/g, function(\$0, \$1)
{
var result = (+\$1).toString(16);