How to Convert an Ipv4 Address into a Integer in C#

Convert IP Address to UInt32/UInt64 in .NET Core


var address = IPAddress.NetworkToHostOrder((int)BitConverter.ToUInt32(IPAddress.Parse(ipAddress).GetAddressBytes(), 0));

How do you parse an IP address string to a uint value in C#?

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.

How do display the Integer value of an IPAddress

This might work, will try it and see.

public double IPAddressToNumber(string IPaddress)
{
int i;
string [] arrDec;
double num = 0;
if (IPaddress == "")
{
return 0;
}
else
{
arrDec = IPaddress.Split('.');
for(i = arrDec.Length - 1; i >= 0 ; i = i -1)
{
num += ((int.Parse(arrDec[i])%256) * Math.Pow(256 ,(3 - i )));
}
return num;
}
}

Convert IP Address into int, (ie inet aton in c#)


public int ToInteger(int A, int B, int C, int D)
{
return Convert.ToInt32((A* Math.Pow(256, 3)) + (B* Math.Pow(256, 2)) + (C* 256) + D);
}

or http://msdn.microsoft.com/en-us/library/system.net.ipaddress.parse.aspx

you can extend your own IP class like I did.

How to convert long decimal to IP Add IPAddress C# work

By hand, using int calculation.

16885952 % 256 = 192
16885952 / 256 % 256 = 168
16885952 / 256 / 256 % 256 = 1
16885952 / 256 / 256 / 256 = 1

convert ip address to a single number

You can do this with a custom comparator for struct sokaddr_in. The snippet below explains what I mean. The advantage of this approach is you can customize the comparator for IPv6 and to include port nos. and other stuff if IP addresses are same.

    #include <iostream>
#include <algorithm>
#include <arpa/inet.h>


struct CompareSockAddr_in
{
bool operator ()(struct sockaddr_in ip1,struct sockaddr_in ip2){
// use return ip1.sin_addr.s_addr < ip2.sin_addr.s_addr; for ascending order
return ip1.sin_addr.s_addr > ip2.sin_addr.s_addr;
}
};

int main()
{
struct sockaddr_in antelope[2];

inet_pton(AF_INET, "10.0.0.2", &(antelope[0].sin_addr));
inet_pton(AF_INET, "60.0.0.4", &(antelope[1].sin_addr));

std::cout<<inet_ntoa(antelope[0].sin_addr)<<std::endl;
std::cout<<inet_ntoa(antelope[1].sin_addr)<<std::endl;
std::sort(antelope,antelope+2,CompareSockAddr_in());

std::cout<<"Sorted List...\n";

std::cout<<inet_ntoa(antelope[0].sin_addr)<<std::endl;
std::cout<<inet_ntoa(antelope[1].sin_addr)<<std::endl;

return 0;
}

Hope this helps.

Generate consecutive IPv4 addresses based on a given integer

The easiest thing to do I think is to use the IPAddress and manipulate the bytes being used to create them. Use ToString() on the returned IP addresses if you need it in string form.

    static void Main(string[] _)
{
var address = IPAddress.Parse("192.168.0.2");

foreach (var ip in GetTestAddresses(address, 300))
{
Console.WriteLine(ip);
}
}

public static IEnumerable<IPAddress> GetTestAddresses(IPAddress start, int count)
{
var bytes = start.GetAddressBytes();

for (int i = 0; i < count; ++i)
{
var part4 = bytes[^1];
var part3 = bytes[^2];

if (part4 < 255)
{
++part4;
}
else if (part3 < 255)
{
part4 = 0;
++part3;
}
else
{
break;
}

bytes[^1] = part4;
bytes[^2] = part3;

yield return new IPAddress(bytes);
}
}

C# - WinRT - Convert IPv4 address from uint to string?

A little "dirty", but seems to work

        uint ip = 0xFFDF5F4F;
var bytes = BitConverter.GetBytes(ip);
string res = string.Join(".", bytes.Reverse());

Output is 255.223.95.79 for this case



Related Topics



Leave a reply



Submit