代码之家  ›  专栏  ›  技术社区  ›  Vivek Shukla

在两个子网之间查找较大的子网(其中一个子网的相邻1较少)

  •  0
  • Vivek Shukla  · 技术社区  · 8 年前

    我有一个代码片段,可以在两个子网掩码之间找到较大的子网。代码片段使用左移位运算符,IMHO使代码不可读(可能是因为我很少使用它,我相信现在很多开发人员都没有使用它)。

    下面是执行此任务的代码。

            IPAddress subNet1 = IPAddress.Parse("255.255.255.0");
            IPAddress subNet2 = IPAddress.Parse("255.255.128.0");
    
            byte[] bytes;
            bytes = subNet1.GetAddressBytes();
            int tempSub1 = Convert.ToInt32((bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]));
    
            bytes = subNet2.GetAddressBytes();
            int tempSub2 = Convert.ToInt32((bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]));
    
            if(tempSub1 == tempSub2)
                Console.WriteLine("Pick Any Subnet");
    
            //Find Larger Subnet(Less number of contiguous 1's)
            if(tempSub1<tempSub2)
                Console.WriteLine("Subnet 1 is Larget Subnet");
            else
                Console.WriteLine("Subnet 2 is Larget Subnet");
    

    我能想到的另一种方法是;为了可读性起见 将IP地址转换为二进制,将值存储在字符串中,然后比较1。

    有哪些方法可以使代码更易于理解(对于不习惯转换运算符的人)?

    2 回复  |  直到 8 年前
        1
  •  2
  •   ikegami Gilles Quénot    8 年前

    这是将字节转换为整数的适当方式。然而 BitConverter 可以掩盖肮脏。

    IPAddress subnet1 = IPAddress.Parse("255.255.255.0");
    IPAddress subnet2 = IPAddress.Parse("255.255.128.0");
    
    int subnet1Mask = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(subnet1.GetAddressBytes(), 0));
    int subnet2Mask = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(subnet2.GetAddressBytes(), 0));
    
    int cmp = -subnet1Mask.CompareTo(subnet2Mask);
    if      (cmp < 0) { Console.WriteLine("Subnet 1 is smaller than Subnet 2."); }
    else if (cmp > 0) { Console.WriteLine("Subnet 1 is larger than Subnet 2."); }
    else              { Console.WriteLine("Subnet 1 and Subnet 2 have the same size."); }
    

    也就是说,您可以简单地比较字节数组,而不是将其转换为整数。

    int compareSubnetSize(IPAddress subnet1, IPAddress subnet2)
    {
        byte[] subnet1MaskBytes = subnet1.GetAddressBytes();
        byte[] subnet2MaskBytes = subnet2.GetAddressBytes();
    
        int cmp = 0;
        for (int i=0; i<4; ++i) {
            cmp = subnet1MaskBytes[i].CompareTo(subnet2MaskBytes[i]);
            if (cmp != 0)
                return -cmp;
        }
    
        return 0;
    }
    
    IPAddress subnet1 = IPAddress.Parse("255.255.255.0");
    IPAddress subnet2 = IPAddress.Parse("255.255.128.0");
    
    int cmp = compareSubnetSize(subnet1, subnet2);
    if      (cmp < 0) { Console.WriteLine("Subnet 1 is smaller than Subnet 2."); }
    else if (cmp > 0) { Console.WriteLine("Subnet 1 is larger than Subnet 2."); }
    else              { Console.WriteLine("Subnet 1 and Subnet 2 have the same size."); }
    
        2
  •  0
  •   Slai    8 年前

    将IPv4地址作为字符串进行比较似乎比转换它们更容易。例如:

    Regex.Replace("255.255.255.0", @"\d+", m => m.Value.PadLeft(3))       //  "255.255.255.  0"
    
    string.Concat("255.255.255.0".Split('.').Select(s => s.PadLeft(4)))   // " 255 255 255   0"
    
    BitConverter.ToString(IPAddress.Parse("255.255.255.0").GetAddressBytes())  // "FF-FF-FF-00"
    
    string.Concat("255.255.255.0".Split('.').Select(s => $"{int.Parse(s):X2}")))  // "FFFFFF00"