示例#1
0
        public override string ToString()
        {
            if (Null)
            {
                return("");
            }
            var s = new System.Text.StringBuilder();

            if (!V6)
            {
                var ipl = Address;
                if (IsMask)
                {
                    int digits = 32 - Cidr;
                    ipl = (Int128.MaxValue << 1) | 0x1; // remove left sign bit
                    ipl = ipl << digits;
                }
                s.Append(String.Format("{0}.{1}.{2}.{3}", (ipl >> 24) & 0xFFL, (ipl >> 16) & 0xFFL, (ipl >> 8) & 0xFFL, (ipl & 0xFFL)));
            }
            else if (!IsMask)
            {
                var    vals = new List <int>();
                int    i;
                Int128 a = Address;
                for (i = 0; i < 8; i++)
                {
                    vals.Add((int)(a % c));
                    a = a / c;
                }

                int index = -1, n = 0, m = 0;
                for (i = 7; i >= 0; i--)
                {
                    if (vals[i] == 0)
                    {
                        n++;
                        if (n > m)
                        {
                            index = i;
                            m     = n;
                        }
                    }
                }
                index += m - 1;

                i = 7;
                while (i >= 0)
                {
                    if (i == index)
                    {
                        if (m == 8)
                        {
                            s.Append("::");
                        }
                        else
                        {
                            s.Append(":");
                        }
                        i -= m;
                    }
                    if (i >= 0)
                    {
                        if (i < 7)
                        {
                            s.Append(":");
                        }
                        s.Append(vals[i].ToString("x"));
                    }
                    i--;
                }
            }
            if (IsSubnet && !(IsMask && V4))
            {
                s.Append('/'); s.Append(Cidr.ToString());
            }
            return(s.ToString());
        }