Home > database >  How can I change this "number" var so it displays a binary number not a "System Tostr
How can I change this "number" var so it displays a binary number not a "System Tostr

Time:03-09

Sorry for the spaghettiOs!

I am trying to do the Adventofcode tasks and this one got me stuck for a bit. I am a newbie into programming but trying my best to finish this task so probably I am doing everything by force. Can u guys help me or point into a right direction?

    static void Main()
    {
        string path = "plik.txt";
        string[] diagnostics = File.ReadAllLines(path);

        int diagSize = diagnostics.Length;                      
        string[] number = new string[12];

        //  int result = Convert.ToInt32(diagnostics[i], 2);
        for (int j = 0; j < 12; j  )
        {
            int ones = 0;
            int zeroes = 0;
            for (int i = 0; i < diagSize; i  )
            {
                string temp = diagnostics[i];

                if (temp[j] == '1')
                {
                    ones  ;
                }
                else zeroes  ;
            }
            if(ones > zeroes)
            {
                number[j] = "1";
            }
            else
            {
                number[j] = "0";
            }
            Console.WriteLine(number[j]);
        }
        Console.WriteLine(number);            
    }             

CodePudding user response:

As suggested on the first comment you received, you should first produce a string from the array you previously built, like this:

string result = String.Join("", number);

Now you can easily print that string or just convert it to decimal if you need it as a number to further process.

CodePudding user response:

something like in the below link?

Convert integer to binary in C#

  • Related