Are there any .NET Framework Method that does INET_ATON()

断了今生、忘了曾经 提交于 2019-12-10 23:57:28

问题


That does this calculation below

address = '174.36.207.186'

( o1, o2, o3, o4 ) = address.split('.')

integer_ip =   ( 16777216 * o1 )
             + (    65536 * o2 )
             + (      256 * o3 )
             +              o4

回答1:


string s = "174.36.207.186";

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



回答2:


You can parse the numbers into a byte array, then use BitConverter.ToInt32 to put them together into an int:

byte[] parts = address.Split('.').Select(Byte.Parse).ToArray();
if (BitConverter.IsLittleEndian) {
  Array.Reverse(parts);
}
int ip = BitConverter.ToInt32(parts, 0);



回答3:


You can parse the string to an IPAddress instance and then access the Address Property:

long result = IPAddress.Parse("174.36.207.186").Address;

Note that this will yield a compiler warning (obsolete property), because it doesn't work with IPv6.



来源:https://stackoverflow.com/questions/13997836/are-there-any-net-framework-method-that-does-inet-aton

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!