-1

Possible Duplicate:
compare two ip with C#

How to compare two IP? By compare I mean to tell whether IP1 is greater that IP2. Is that possible? As I see, IPAddress does not have that functionality.

Community
  • 1
  • 1
seeker
  • 3,025
  • 6
  • 33
  • 63
  • I tried to compare string representation, but as it turned out, it's not unique. – seeker Oct 16 '12 at 14:40
  • well NaNNy provided a solution for equality, but as far as I understood thats not what you need right? – DerApe Oct 16 '12 at 14:41
  • 7
    How do you *want* to define greater than? Addresses from different families have no obvious ordering relationship between them. Why do you need to perform this comparison? Even within a family, comparing two addresses doesn't seem to make much sense. – Damien_The_Unbeliever Oct 16 '12 at 14:42
  • I want to enumerate through IPs as 194.44.44.44,194.44.44.45,...,194.44.44.255,194.44.45.0 and so on – seeker Oct 16 '12 at 14:43
  • and greater may be defined as follows: first triplet of IP that is greater than the same triplet in other IP automatically means that IP1 is greater. – seeker Oct 16 '12 at 14:44
  • So, is the question you *should* have asked "how do I enumerate a range of IP addresses, given a start address and an end address", or possible "how do I enumerate a subnet"? – Damien_The_Unbeliever Oct 16 '12 at 14:46
  • @Damien_The_Unbeliever you're right. – seeker Oct 16 '12 at 14:46
  • try the string compare solution given by me in my answer – Pranay Rana Oct 16 '12 at 14:48

2 Answers2

0

edit: see the answer here for something far more elegant. Heed the warning about endian-ness, though.

Pranay Rana's answer is fundamentally broken: comparing '11.2.3.4' and '1.12.3.4' will show that they are are equal. They're clearly not.

IP addresses are essentially 32 bit integers in a particular form. You can use this fact to write a trivial function that takes the string value and gives you the integer, which is easier to compare against:

static void Main(string[] args)
{
    string ip1 = "11.2.3.4";
    string ip2 = "1.12.3.4";
    uint ipInt1 = ipAddressToInt(ip1);
    uint ipInt2 = ipAddressToInt(ip2);
    Console.WriteLine(ipInt1 < ipInt2);
    Console.ReadLine();
}

private static uint ipAddressToInt(string ip)
{
    uint retVal;
    System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse(ip);
    byte[] IPBytes = ipAddress.GetAddressBytes(); 

    retVal = (uint)IPBytes[3] << 24;
    retVal += (uint)IPBytes[2] << 16;
    retVal += (uint)IPBytes[1] << 8;
    retVal += (uint)IPBytes[0]; 
    return retVal;
}

Note the System.Net.IPAddress.Parse in the ipAddressToInt function. This validates the input string before working with it.

Community
  • 1
  • 1
Reacher Gilt
  • 1,753
  • 11
  • 24
  • hi i updated my answer and it will work the for the condition you given... – Pranay Rana Oct 16 '12 at 15:44
  • You can't convert strings to longs with `long ip1 = ip1.Replace(".", "");`. If anything, it's even more broken now than before. – Reacher Gilt Oct 16 '12 at 15:48
  • i updated my answer after words you can check it ....check top one which is latest .. – Pranay Rana Oct 16 '12 at 15:55
  • Ehh. Here's an answer that makes both of ours look terrible: http://stackoverflow.com/questions/461742/how-to-convert-an-ipv4-address-into-a-integer-in-c – Reacher Gilt Oct 16 '12 at 16:04
  • 1
    I'd disagree with your byte significances here. If you make the fourth byte most significant, you end up e.g. sorting `11.0.0.10` between `192.168.1.1` and `192.168.1.15` – Rawling Oct 16 '12 at 16:05
  • 1
    (That link in your last comment makes the same mistake - or rather, relies on the host machine's endianness - and also makes the mistake of using a _signed_ `int`, again messing with any sorting you might do.) – Rawling Oct 16 '12 at 16:10
  • That was exactly my issue, I was running into an endian problem. Assumptions, eh? – Reacher Gilt Oct 16 '12 at 16:13
-1

EDIT (This might work for all condition which is specified in comments )

string ip1= "1.2.3.4";
string ip2 ="5.6.7.8";

string[] ip1S = ip1.Split(new char[] {'.'});
string[] ip2S = ip2.Split(new char[] {'.'});

for(int i=0;i<4;i++)
{
  if(Convert.ToInt32(ip1S[i]) > Convert.ToInt32(ip2S[i]))
  {
    Console.WriteLine("ip1 is higher");
    break;
  }
  else if(Convert.ToInt32(ip2S[i]) > Convert.ToInt32(ip1S[i])) 
   {
    Console.WriteLine("ip2 is higher");
    break;
  }  
}

Old

you can do string comapre like this

string ip1= "1.2.3.4";
string ip2 ="5.6.7.8";

string ip1R = ip1.Replace(".","");
string ip2R = ip2.Replace(".","");


Console.WriteLine(String.Compare(ip1R ,ip2R ));

output

A negative integer    str1 is less than str2.
0                     str1 equals str2.
A positive integer    This instance is greater than value.
-or-
1

For more change string compare on MSDN : http://msdn.microsoft.com/en-us/library/fbh501kz(v=vs.80).aspx

Pranay Rana
  • 164,177
  • 33
  • 228
  • 256