Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## Calculate IP Length

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.

• 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?

• 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

• So, you have several string variables defined in JavaScript? Like "10.52.28.0", "10.52.29.254"...?

Could be something like this:
Code:
```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;
}```

• ## Users who have thanked Kor for this post:

Leos (07-11-2011)

• Hello

Thanks for suggestions.
I appreciate this.

I have error that n is = NaN

it looks like it doesn't recognize "numbers"

hmm

• There must be a typo in your code. The function works fine although it has a minor flaw
Code:
```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)

• ## Users who have thanked devnull69 for this post:

Leos (07-12-2011)

• Originally Posted by devnull69
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.

• ## Users who have thanked Krupski for this post:

Leos (07-13-2011)

• That's what the split('.') is for ... the number of digits is not relevant there. Number() will turn them into a numeric type anyway

• ## Users who have thanked devnull69 for this post:

Leos (07-13-2011)

• 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;
}
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.

• ## Users who have thanked Old Pedant for this post:

Leos (07-13-2011)

• I'm investigatin your proposition in my soft.
I'll give info does it works or not
THANKS !

• Originally Posted by Leos
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:
Code:
```<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
Code:
`var x1=10.52;`
this is a string:
Code:
```var x2='10.52.28.0';
//or
var x3="10.52.28.0";```
Strings are quoted. Numbers are not.

• ## Users who have thanked Kor for this post:

Leos (08-01-2011)

• Originally Posted by Old Pedant
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 ?

• Originally Posted by devnull69
There must be a typo in your code. The function works fine although it has a minor flaw
Code:
```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

Code:
```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 ;/

• 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.

• 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...

•
Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•