2012-05-10 6 views
0

서브넷을 사용하여 IP 범위를 시작 IP로 변환해야합니다. 예를 들어서브넷을 사용하여 ip 범위를 ip로 변환합니다.

입력 :

1.1.1.0 - 1.1.1.255 
(255.255.255.0) 

출력 :

1.1.1.0/24 

덕분에,

+1

모든 범위에 적절한 CIDR 넷 마스크가있는 것은 아닙니다. 어떻게 그걸 처리하려고합니까? – sarnold

+0

: 1.1.1.3 - 1.1.1.5에 대해 예상되는 결과는 무엇입니까? – Vlad

+0

참조로, 이것은 당신이 요구하는 것과 반대입니다 : http://stackoverflow.com/questions/1470792/how-to-calculate-the-ip-range-when-the-ip-address-and - the-netmask-is-given – BeemerGuy

답변

0

그것은 비트 연산자를 사용하여 쉽게이 :

byte[] ip1 = {1, 1, 1, 0}; 
byte[] ip2 = {1, 1, 1, 255}; 
byte[] omask = 
{ 
    (byte)(ip1[0]^ip2[0]), 
    (byte)(ip1[1]^ip2[1]), 
    (byte)(ip1[2]^ip2[2]), 
    (byte)(ip1[3]^ip2[3]) 
}; 
string mask = Convert.ToString(omask[0], 2) + Convert.ToString(omask[1], 2) 
       + Convert.ToString(omask[2], 2) + Convert.ToString(omask[3], 2); 
// count the number of 1's in the mask. Substract to 32: 
// NOTE: the mask doesn't have all the zeroes on the left, but it doesn't matter 
int bitsInMask = 32 - (mask.Length - mask.IndexOf("1")); // this is the /n part 
byte[] address = 
{ 
    (byte)(ip1[0] & ip2[0]), 
    (byte)(ip1[1] & ip2[1]), 
    (byte)(ip1[2] & ip2[2]), 
    (byte)(ip1[3] & ip2[3]) 
}; 
string cidr = address[0] + "." + address[1] + "." 
    + address[2] + "." + address[3] + "/" + bitsInMask; 

CIDR이 당신에게 제공 네트워크 r ank.

관련 문제