开发者

.NET Equivalant for INET_NTOA and INET_ATON

Any C#/.Net equivalent methods, or managed code examples for INET_NTOA and INE开发者_如何学JAVAT_ATON?


The IPAddress class has static methods:

HostToNetworkOrder
NetworkToHostOrder 

With various overloads.


Have a look at this forum thread:

http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/00a001af-e01d-4590-82c1-1f6142eb8c34


Just to clarify, you're looking to parse a string representation of an IP Address, to an IPAddress object?

(That's my understanding of this article explaining INET_NTOA)

In that case ,it's System.Net.IPAddress.Parse("127.0.0.1"), and you can use the .ToString() off an IPAddress to get the string rep back out.


To make NTOA compatible with MySQL i had to do a Endian conversion

byte[] ip = BitConverter.GetBytes(ipInt);
Array.Reverse(ip);

IPAddress = new IPAddress(BitConverter.ToUInt32(ip,0))


found this here:

string s = "174.36.207.186";

uint i = s.Split('.')
          .Select(uint.Parse)
          .Aggregate((a, b) => a * 256 + b);


This is equivalent to INET_ATON and INET_NTOA in MySQL

public static uint INetA2N(string ip)
{
    try
    {
        uint result = ip.Split('.')
                        .Select(uint.Parse)
                        .Aggregate((a, b) => a * 256 + b);
        return result;
    }
    catch (System.Exception)
    {

        return 0;
    }
}

public static string InetN2A(uint ipValue)
{
    if (ipValue > 4294967295 || ipValue < 0)
        return string.Empty;

    return ((ipValue & 0xFF000000) / 16777216).ToString() + "." +
           ((ipValue & 0x00FF0000) / 65536).ToString() + "." +
           ((ipValue & 0x0000FF00) / 256).ToString() + "." +
           (ipValue & 0x000000FF).ToString();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜