private void OnSelectionChanged(object sender, SelectionChangedEventArgs e) { if (CIDR.SelectedItem != null) { ComboBoxItem a = (ComboBoxItem)CIDR.SelectedItem; int cidrM = int.Parse(a.Content.ToString()); IP_Mask mask = IPmain.GetIP_Mask(cidrM); string valm = String.Format("{0}.{1}.{2}.{3}", mask.f_octet, mask.s_octet, mask.t_octet, mask.fo_octet); Mask.Text = valm; } }
public static IP_Mask GetIP_Mask(int cidr) { IP_Mask mask; byte[] bit_decimal = new byte[] { 0, 128, 192, 224, 240, 248, 252, 254, 255 }; if (cidr <= 8) { byte first = bit_decimal[cidr]; mask = new IP_Mask(first, 0, 0, 0); } else if (cidr > 8 && cidr <= 16) { int pwr = cidr - 8; byte second = bit_decimal[pwr]; mask = new IP_Mask(255, second, 0, 0); } else if (cidr > 16 && cidr <= 24) { int pwr = cidr - 16; byte third = bit_decimal[pwr]; mask = new IP_Mask(255, 255, third, 0); } else if (cidr > 24 && cidr <= 32) { int pwr = cidr - 24; byte fourth = bit_decimal[pwr]; mask = new IP_Mask(255, 255, 255, fourth); } else { mask = new IP_Mask(0, 0, 0, 0); } return(mask); }