Formula - How many address in a range
-
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!
-
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!
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 )
-
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 )
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.
-
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.
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 )
-
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!
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
-
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
WOW exactly what i needed!!!! THANK YOU!!!