0

Example:user will enter ffff:0000:ffff:ffff and out put must be like this:

1111111111111111:0000000000000000:1111111111111111:1111111111111111

I need it in c# abbreviate example:

1111111111111111:::000000000000:1111111111111111:1111111111111111
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

0

Try this for your particular case

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);
        }
    }
}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • thats working but now one more thing abbreviating... :( – Baqir Bajwa Jul 04 '15 at 20:15
  • output = output.Substring(0, 16) + "::" + output.Substring(16); – jdweng Jul 04 '15 at 20:35
  • aggregating not working... rule for agree-gate: if a complete portion is zero then it can be written as 0 and if two or more consective portions are zero they can be ignored for example if we have 1234 0000 0000 0000 1212 it can be writeen as 1234 :: 1212 – Baqir Bajwa Jul 06 '15 at 18:31