首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IPv6缩写(零块压缩)逻辑。我用的是c#

IPv6缩写(零块压缩)逻辑。我用的是c#
EN

Stack Overflow用户
提问于 2015-07-01 20:22:46
回答 2查看 1.3K关注 0票数 1

这是一个完整的未压缩IP地址2001:0008:0000: code 30:0000:0000:0000:0101我需要像这样压缩它:8:0:code 30::101,但我只能用下面的代码将零压缩成这样的块:8:0:code 30:0:0:0:101

代码语言:javascript
复制
 string output = "";
        string a = textBox1.Text;
        if (a.Length != 39  )
            MessageBox.Show("Invalid IP please enter the IPv6 IP in this format 6cd9:a87a:ad46:0005:ad40:0000:5698:8ab8");
        else
        {
            for (int i = 0; i < a.Length; i++)
            {
                if ((a[i] >= '1' && a[i] <= '9') || (Char.ToLower(a[i]) >= 'a' && Char.ToLower(a[i]) <= 'f') || ((i + 1) % 5 == 0 && a[i] == ':'))
                {
                    output = output + a[i];
                }
                else if ((a[i]=='0' && a[i-1]==':') || (a[i]=='0' && a[i-1]=='0' && a[i-2]==':') || (a[i]=='0' && a[i-1]=='0' && a[i-2]=='0' && a[i-3]==':')) 
                {

                }
                else if (a[i] == '0')
                {
                    output = output + a[i];
                }

                else
                {
                    MessageBox.Show("Invalid IP please enter the IPv6 IP in this format 6cd9:a87a:ad46:0005:ad40:0000:5698:8ab8");
                }
            }

            textBox2.Text = output;
        }

我正在使用c#,但我只需要编程逻辑,关于如何删除整个零块,问题是可能有超过1组块包含ip中的所有零,但只有一个应该缩写。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-01 21:26:08

比我预期的要复杂得多,但是在这里,您可以使用正则表达式来完成它:

代码语言:javascript
复制
        private static string Compress(string ip)
        {
            var removedExtraZeros = ip.Replace("0000","*");

            //2001:0008:*:CD30:*:*:*:0101
            var blocks = ip.Split(':');

            var regex = new Regex(":0+");
            removedExtraZeros = regex.Replace(removedExtraZeros, ":");


            //2001:8:*:CD30:*:*:*:101

            var regex2 = new Regex(":\\*:\\*(:\\*)+:");
            removedExtraZeros = regex2.Replace(removedExtraZeros, "::");
            //2001:8:*:CD30::101

            return removedExtraZeros.Replace("*", "0");
        }
票数 2
EN

Stack Overflow用户

发布于 2020-11-20 17:40:01

如果您想不使用Regex实现相同的结果

代码语言:javascript
复制
public string Compress(string value)
{
    var values = value.Split(",");
    var ints = values.Select(i => int.Parse(i, System.Globalization.NumberStyles.HexNumber));
    var result = ints.Select(Conversion.Hex);
    return string.Join(":", result);
}

没有花时间在微优化(stackalloc,spans等)上但这给出了一个想法。对于优化方面的参考,您可以查看IPAddress.Parse在.net核心中的实现。请注意,IPAddress.Parse的结果将给出与上面的示例不同的结果:

代码语言:javascript
复制
Compress        -> 2001:8:0:CD30:0:0:0:101 
IPAddress.Parse -> 2001:8:0:cd30::101

这可以通过进入一些对象和其他想法来“清理”。

编辑:

在和我的一个同事聊天之后,我花了一些时间写了一个“优化”的版本。我没有花时间清理代码,所以将来的编辑可能会更干净。

代码语言:javascript
复制
public string Compress(string value)
{
    Span<char> chars = stackalloc char[value.Length];
    const char zero = '0';
    const char colon = ':';
    int index = 0;
    int positionInSegment = 0;
    bool startsWithZero;

    while (index < _originalValue.Length)
    {
        startsWithZero = value[index] == zero && positionInSegment == 0;
        positionInSegment++;
        if (startsWithZero)
        {
            if (index == value.Length - 1)
            {
                chars[index] = zero;
                break;
            }
                        
            if (value[index + 1] == colon)
            {
                chars[index] = zero;
                positionInSegment = 0;
                index++;
                continue;
            }
                        
            positionInSegment = 0;
            index++;
            continue;
        }

        if (value[index] == colon)
        {
            positionInSegment = 0;
            chars[index] = colon;
            index++;
            continue;
        }
                    
        chars[index] = value[index];
        index++;
    }

    return chars.ToString();
}

我还为将来的参考资料创建了一个公共依据:https://gist.github.com/DeanMilojevic/7b4f1d060ce8cfa191592694b11234d7

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31170946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档