Il est facile à l'aide des opérateurs au niveau du bit:
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 vous donne la réseau r ank.
Toutes les plages n'auront pas de masques de réseau CIDR appropriés; comment avez-vous l'intention de gérer ceux-ci? – sarnold
pour clarifier: que prévoyez-vous d'être produit pour 1.1.1.3 - 1.1.1.5? – Vlad
À titre de référence, c'est le contraire de ce que vous demandez: http://stackoverflow.com/questions/1470792/how-to-calculate-the-ip-range-when-the-ip-address-and -the-netmask-is-given – BeemerGuy