문제

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
도움이 되었습니까?

해결책

string s = "174.36.207.186";

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

다른 팁

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);

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top