Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Formula - How many address in a range

Formula - How many address in a range

Scheduled Pinned Locked Moved C#
helptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    StevenWalsh
    wrote on last edited by
    #1

    This should be easy, however I keep getting it wrong :( I'm completely missing something, and google isn't being much help. I guess its to late... not sure. I need to calculate how many addresses are in a range. So what i'm looking for is a formula to find this. example i have 10.126.0.1 through 10.127.0.1 i want to know how many addresses are in between the range. Thanks!

    C L 2 Replies Last reply
    0
    • S StevenWalsh

      This should be easy, however I keep getting it wrong :( I'm completely missing something, and google isn't being much help. I guess its to late... not sure. I need to calculate how many addresses are in a range. So what i'm looking for is a formula to find this. example i have 10.126.0.1 through 10.127.0.1 i want to know how many addresses are in between the range. Thanks!

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      One presumes you need to take an IP address, split it into four numbers, and check if each of those numbers is within the ranges specified by doing the same to your range IP addresses. string [] lower = "10.126.0.1".Split(new char [] {'.'} ); string [] upper= "10.127.0.1".Split(new char [] {'.'} ); string [] range = newIp.Split(new char[] {'.'} ); // check here that range contains 4 strings bool match = true; for(int i=0;i<4;++i) { match &= (int.TryParse(lower[i]) >= int.TryParse(range[i])); match &= (int.TryParse(upper[i]) <= int.TryParse(range[i])); } Something like that would work, but it has levels of nastiness. 1 - it assumes the values coming in are all valid numbers. Using TryParse would add a lot of code, you could add a try/catch ( if it blows up, they can't match ). 2 - it parses range[i] twice, instead of storing it. But, it should work, and should give you a good starting point. Assuming my assumptions about the rules that define a number being 'in the range' are correct.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      S 1 Reply Last reply
      0
      • C Christian Graus

        One presumes you need to take an IP address, split it into four numbers, and check if each of those numbers is within the ranges specified by doing the same to your range IP addresses. string [] lower = "10.126.0.1".Split(new char [] {'.'} ); string [] upper= "10.127.0.1".Split(new char [] {'.'} ); string [] range = newIp.Split(new char[] {'.'} ); // check here that range contains 4 strings bool match = true; for(int i=0;i<4;++i) { match &= (int.TryParse(lower[i]) >= int.TryParse(range[i])); match &= (int.TryParse(upper[i]) <= int.TryParse(range[i])); } Something like that would work, but it has levels of nastiness. 1 - it assumes the values coming in are all valid numbers. Using TryParse would add a lot of code, you could add a try/catch ( if it blows up, they can't match ). 2 - it parses range[i] twice, instead of storing it. But, it should work, and should give you a good starting point. Assuming my assumptions about the rules that define a number being 'in the range' are correct.

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        S Offline
        S Offline
        StevenWalsh
        wrote on last edited by
        #3

        First, I really appreciate the reply! Your suggestion might work. (i verify the numbers are in range before the program reaches this point so i'm not worried about it being outside) and I have already converted the numbers to an array of 4 integers when its time to calculate the total, however it's quitting time for me now, so i'll have to wait until tomorrow to try it. But in the mean time if anyone else has a suggestion I'd still rather use a formula for the calculation.

        C 1 Reply Last reply
        0
        • S StevenWalsh

          First, I really appreciate the reply! Your suggestion might work. (i verify the numbers are in range before the program reaches this point so i'm not worried about it being outside) and I have already converted the numbers to an array of 4 integers when its time to calculate the total, however it's quitting time for me now, so i'll have to wait until tomorrow to try it. But in the mean time if anyone else has a suggestion I'd still rather use a formula for the calculation.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          What about a method like int GetIPasint(string ipAddress) { string [] ip = ipAddress.Split(new char[] { '.' } ); int ipAddress = 0; for(int i = 0; i < 4; ++i) { int portion; if (int.TryParse(ip[i], out portion) { ipAddress += portion << (4-i); } } return ipAddress; } Hopefully, that would work to give you values that fall between the ranges. Although, I have some doubts, I have to say, it's worth a shot.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          1 Reply Last reply
          0
          • S StevenWalsh

            This should be easy, however I keep getting it wrong :( I'm completely missing something, and google isn't being much help. I guess its to late... not sure. I need to calculate how many addresses are in a range. So what i'm looking for is a formula to find this. example i have 10.126.0.1 through 10.127.0.1 i want to know how many addresses are in between the range. Thanks!

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, the number of addresses in the interval[ a.b.c.d , e.f.g.h ] equals num32(e,f,g,h) - num32(a,b,c,d) + 1 where num32 is the 32-bit number formed by those 4 byte values, as in: num32(a,b,c,d) = ((((((a<<8)+b)<<8)+c)<<8)+d); BTW: you may save a few lines of code using IPAddress.GetAddressBytes(), unfortunately there seems to be no method IPAddress.GetAddressInt32() ! :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


            S 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, the number of addresses in the interval[ a.b.c.d , e.f.g.h ] equals num32(e,f,g,h) - num32(a,b,c,d) + 1 where num32 is the 32-bit number formed by those 4 byte values, as in: num32(a,b,c,d) = ((((((a<<8)+b)<<8)+c)<<8)+d); BTW: you may save a few lines of code using IPAddress.GetAddressBytes(), unfortunately there seems to be no method IPAddress.GetAddressInt32() ! :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


              S Offline
              S Offline
              StevenWalsh
              wrote on last edited by
              #6

              WOW exactly what i needed!!!! THANK YOU!!!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups