7

I'm writing C# code that uses the windows IP Helper API. One of the functions I'm trying to call is "GetBestInterface" that takes a 'uint' representation of an IP. What I need is to parse a textual representation of the IP to create the 'uint' representation.

I've found some examples via Google, like this one or this one, but I'm pretty sure there should be a standard way to achieve this with .NET. Only problem is, I can't find this standard way. IPAddress.Parse seems to be in the right direction, but it doesn't supply any way of getting a 'uint' representation...

There is also a way of doing this using IP Helper, using the ParseNetworkString, but again, I'd rather use .NET - I believe the less I rely on pInvoke the better.

So, anyone knows of a standard way to do this in .NET?

Groo
  • 45,930
  • 15
  • 109
  • 179
Hershi
  • 1,950
  • 2
  • 17
  • 23
  • possible duplicate of [How to convert an IPv4 address into a integer in C#](http://stackoverflow.com/questions/461742/how-to-convert-an-ipv4-address-into-a-integer-in-c) as [nt. pointed out](http://stackoverflow.com/a/37231/588306)? – Deanna Aug 10 '12 at 08:39

9 Answers9

18

Shouldn't it be:

var ipAddress = IPAddress.Parse("some.ip.address");
var ipBytes = ipAddress.GetAddressBytes();
var ip = (uint)ipBytes [0] << 24;
ip += (uint)ipBytes [1] << 16;
ip += (uint)ipBytes [2] <<8;
ip += (uint)ipBytes [3];

?

Christo
  • 181
  • 1
  • 2
12

MSDN says that IPAddress.Address property (which returns numeric representation of IP address) is obsolete and you should use GetAddressBytes method.

You can convert IP address to numeric value using following code:

var ipAddress = IPAddress.Parse("some.ip.address");
var ipBytes = ipAddress.GetAddressBytes();
var ip = (uint)ipBytes [3] << 24;
ip += (uint)ipBytes [2] << 16;
ip += (uint)ipBytes [1] <<8;
ip += (uint)ipBytes [0];

EDIT:
As other commenters noticed above-mentioned code is for IPv4 addresses only. IPv6 address is 128 bits long so it's impossible to convert it to 'uint' as question's author wanted.

aku
  • 115,356
  • 31
  • 164
  • 200
  • 3
    Are you sure about the four indices' order? it seems to me that byte with index 0 should be shifted 24 bits and not the one with index 3.. – Andrei Rînea Feb 08 '10 at 20:16
11
var ipuint32 = BitConverter.ToUInt32(IPAddress.Parse("some.ip.address.ipv4").GetAddressBytes(), 0);`

This solution is easier to read than manual bit shifting.

See How to convert an IPv4 address into a integer in C#?

Community
  • 1
  • 1
nimish
  • 4,110
  • 3
  • 18
  • 33
  • BitConvertor.ToUINt32() takes two parameters (see http://msdn.microsoft.com/en-us/library/system.bitconverter.toint32.aspx) – mjv Nov 25 '09 at 06:06
  • Whoever tried to edit this answer please don't try editing code, even if incorrect because of typo just add a comment to inform the user who the answer belongs to, and don't go deleting the rest of the answer to just say why you did it. – Popeye Apr 29 '13 at 12:32
5

Also you should remember that IPv4 and IPv6 are different lengths.

Corin Blaikie
  • 16,511
  • 9
  • 33
  • 38
1

Byte arithmetic is discouraged, as it relies on all IPs being 4-octet ones.

Dmitry Shechtman
  • 5,898
  • 4
  • 23
  • 25
1
System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse("192.168.1.1");

byte[] bytes = ipAddress.GetAddressBytes();
for (int i = 0; i < bytes.Length ; i++)
       Console.WriteLine(bytes[i]);

Output will be 192 168 1 1

qasali
  • 11
  • 2
1

Complete solution:

public static uint IpStringToUint(string ipString)
{
    var ipAddress = IPAddress.Parse(ipString);
    var ipBytes = ipAddress.GetAddressBytes();
    var ip = (uint)ipBytes [0] << 24;
    ip += (uint)ipBytes [1] << 16;
    ip += (uint)ipBytes [2] <<8;
    ip += (uint)ipBytes [3];
    return ip;
}

public static string IpUintToString(uint ipUint)
{
    var ipBytes = BitConverter.GetBytes(ipUint);
    var ipBytesRevert = new byte[4];
    ipBytesRevert[0] = ipBytes[3];
    ipBytesRevert[1] = ipBytes[2];
    ipBytesRevert[2] = ipBytes[1];
    ipBytesRevert[3] = ipBytes[0];
    return new IPAddress(ipBytesRevert).ToString();
}

Reverse order of bytes:

public static uint IpStringToUint(string ipString)
{
    return BitConverter.ToUInt32(IPAddress.Parse(ipString).GetAddressBytes(), 0);
}

public static string IpUintToString(uint ipUint)
{
    return new IPAddress(BitConverter.GetBytes(ipUint)).ToString();
}

You can test here:

https://www.browserling.com/tools/dec-to-ip

http://www.smartconversion.com/unit_conversion/IP_Address_Converter.aspx

http://www.silisoftware.com/tools/ipconverter.php

Pavel Samoylenko
  • 441
  • 7
  • 13
1

Correct solution that observes Endianness:

var ipBytes = ip.GetAddressBytes();
ulong ip = 0;
if (BitConverter.IsLittleEndian)
{
    ip = (uint) ipBytes[0] << 24;
    ip += (uint) ipBytes[1] << 16;
    ip += (uint) ipBytes[2] << 8;
    ip += (uint) ipBytes[3];
}
else
{
    ip = (uint)ipBytes [3] << 24;
    ip += (uint)ipBytes [2] << 16;
    ip += (uint)ipBytes [1] <<8;
    ip += (uint)ipBytes [0];
}
Nick
  • 724
  • 8
  • 12
0

I have never found a clean solution (i.e.: a class / method in the .NET Framework) for this problem. I guess it just isn't available except the solutions / examples you provided or Aku's example. :(

Andrei Rînea
  • 18,961
  • 16
  • 112
  • 162