.NET Equivalant for INET_NTOA and INET_ATON

前端 未结 5 929
暖寄归人
暖寄归人 2021-01-25 05:14

Any C#/.Net equivalent methods, or managed code examples for INET_NTOA and INET_ATON?

相关标签:
5条回答
  • 2021-01-25 05:39

    found this here:

    string s = "174.36.207.186";
    
    uint i = s.Split('.')
              .Select(uint.Parse)
              .Aggregate((a, b) => a * 256 + b);
    
    0 讨论(0)
  • 2021-01-25 05:42

    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.

    0 讨论(0)
  • 2021-01-25 05:55

    Have a look at this forum thread:

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

    0 讨论(0)
  • 2021-01-25 05:55

    The IPAddress class has static methods:

    HostToNetworkOrder
    NetworkToHostOrder 
    

    With various overloads.

    0 讨论(0)
  • 2021-01-25 05:56

    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))
    
    0 讨论(0)
提交回复
热议问题