Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    New Coder
    Join Date
    May 2011
    Location
    Europe
    Posts
    26
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Calculate IP Length

    Hello.

    I wonder is there any javascript to calculate IP length

    For example I have

    Start ADDR:

    VLAN_ADDR which is:

    10.52.28.0

    Stop ADDR:

    VLAN_LAST which is

    10.52.29.254

    As I know:result is 512


    or another example:

    Start ADDR:

    VLAN_ADDR which is:

    10.52.64.11

    Stop ADDR:

    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.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    May 2011
    Location
    Europe
    Posts
    26
    Thanks
    20
    Thanked 0 Times in 0 Posts
    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
    Last edited by Leos; 07-11-2011 at 02:14 PM.

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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;
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    Leos (07-11-2011)

  • #5
    New Coder
    Join Date
    May 2011
    Location
    Europe
    Posts
    26
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Hello

    Thanks for suggestions.
    I appreciate this.

    I have error that n is = NaN

    it looks like it doesn't recognize "numbers"

    hmm

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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;
    }
    
    alert(countIP('10.52.28.0', '10.52.29.254'));
    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)

  • #7
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by devnull69 View Post
    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.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • Users who have thanked Krupski for this post:

    Leos (07-13-2011)

  • #8
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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)

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    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;
    }
    alert(ipDiff('10.52.28.0', '10.52.29.254'));
    alert(ipDiff('10.52.29.254', '10.52.28.0'));
    </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)

  • #10
    New Coder
    Join Date
    May 2011
    Location
    Europe
    Posts
    26
    Thanks
    20
    Thanked 0 Times in 0 Posts
    I'm investigatin your proposition in my soft.
    I'll give info does it works or not
    THANKS !

  • #11
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Leos View Post
    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');
    alert(nr)
    </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.
    Last edited by Kor; 07-14-2011 at 06:42 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    Leos (08-01-2011)

  • #12
    New Coder
    Join Date
    May 2011
    Location
    Europe
    Posts
    26
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    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 ?

  • #13
    New Coder
    Join Date
    May 2011
    Location
    Europe
    Posts
    26
    Thanks
    20
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    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;
    }
    
    alert(countIP('10.52.28.0', '10.52.29.254'));
    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 ;/
    Last edited by Leos; 08-18-2011 at 11:48 AM.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    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.

  • #15
    New Coder
    Join Date
    May 2011
    Location
    Europe
    Posts
    26
    Thanks
    20
    Thanked 0 Times in 0 Posts
    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 LastLast

    Posting Permissions

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