【问题标题】:How to convert ipv6 to binary and abbreviate it?如何将ipv6转换为二进制并缩写?
【发布时间】:2015-09-22 07:56:38
【问题描述】:

例子:用户输入ffff:0000:ffff:ffff,输出必须是这样的:

1111111111111111:0000000000000000:1111111111111111:1111111111111111

我在 C# 中需要它 缩写示例:

1111111111111111:::000000000000:1111111111111111:1111111111111111

【问题讨论】:

  • 所以你想把base 16转换成二进制?如果是这样,请参阅:stackoverflow.com/questions/6617284/…
  • 它不工作我需要通过类方法。
  • 你能解释一下缩写例子的规则吗?
  • 用户将以这种形式输入 ip 地址 ffff:192a:0000:aaaa 和冒号(:)
  • #Jezrael 如果用户输入 ffff:0000:0000:ffff this 那么它的缩写形式应该是这个 ffff::ffff

标签: c# c#-3.0


【解决方案1】:

根据您的具体情况试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] hex = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1011", "1110", "1111" };
            string input = "";
            string ainput = "";
            string output = "";

            input = "ffff:0000:ffff:ffff";
            ainput = Aggregating(input);
            output = string.Join("", ainput.ToCharArray().Select(x => x == ':' ? ":" : hex[int.Parse(x.ToString(), NumberStyles.HexNumber)]));
            Console.WriteLine("input : {0}, A input = {1}, output = {2}", input, ainput, output);


            input = "0000:0000:0000:ffff:0000:0000";
            ainput = Aggregating(input);
            output = string.Join("", ainput.ToCharArray().Select(x => x == ':' ? ":" : hex[int.Parse(x.ToString(), NumberStyles.HexNumber)]));
            Console.WriteLine("input : {0}, A input = {1}, output = {2}", input, ainput, output);

            input = "1234:0000:0000:0000:1212";
            ainput = Aggregating(input);
            output = string.Join("", ainput.ToCharArray().Select(x => x == ':' ? ":" : hex[int.Parse(x.ToString(), NumberStyles.HexNumber)]));
            Console.WriteLine("input : {0}, A input = {1}, output = {2}", input, ainput, output);

            Console.ReadLine();
        }
        static string Aggregating(string input)
        {
            List<string> results = new List<string>();
            string[] array = input.Split(new char[] { ':' });

            int zeroCount = 0; //number of consecutive zeroes
            for(int i = 0; i < array.Length; i++)
            {
                if (array[i] == "0000")
                {
                    //if last aggregate
                    if (i == array.Length - 1)
                    {
                        //write zeroes if last aggregate wasn't zero
                        if (zeroCount == 0) results.Add("0000");
                    }
                    else
                    {
                        //item zero so increment consectuive zero count
                        zeroCount++;
                    }
                }
                else
                {
                    //if only last aggregate was zero
                    if (zeroCount == 1) results.Add("0000");
                    //add current aggregate
                    results.Add(array[i]);
                    //reset consecutive zeroes to 0 since current aggregate isn't zero
                    zeroCount = 0;
                }
            }

            return string.Join(":",results);
        }
    }
}
​

【讨论】:

  • 这行得通,但现在还有一件事要缩写... :(
  • 输出 = output.Substring(0, 16) + "::" + output.Substring(16);
  • 聚合不起作用... 同意门规则:如果完整部分为零,则可以写为 0,如果两个或更多连续部分为零,则可以忽略它们,例如,如果我们有 1234 0000 0000 0000 1212 可以写成 1234 :: 1212
猜你喜欢
  • 2023-03-25
  • 2013-11-16
  • 2018-05-01
  • 2010-11-10
  • 2011-03-27
  • 2013-07-26
  • 2013-03-10
  • 1970-01-01
相关资源
最近更新 更多