0

I'm developing code to perform an elementary cellular automata in C#. I'm working in Microsoft Visual Studio Express 2012.

Overall my code is working, but I'm have trouble when writing the results to the console.

I've written a class, "bitrep", which includes a method "brep" which takes uint as an input and returns a string which corresponds to the bit representation of that uint.

For the purpose of this problem I want the returned string to be a black (filled) squares for 1's and an white (empty) square for 0's.

class bitrep
{
    private uint t = 1;
    private int i = 0;
    private StringBuilder val = new StringBuilder();
    public string brep(uint n)
    {
        val.Clear();
        for (i = 31; i >= 0; i--)
        {
            test = (t & n >> i) == 1;
            if (test)
                val.Append((char)9632); // Produce a solid square for 1's
            else
                val.Append((char)9633); // Produce an empty square for 0's.
        }
        return val.ToString();
    }
}

When the program is run with the above code an example of the output is as follows:

    ???????????????■????????????????
    ??????????????■■■???????????????
    Press any key to continue . . .

Where the solid/black squares represent 1's, but instead of white squares, 0's are being written as ?'s.

Why isn't the desired output of white squares (which has a Unicode UTF-16 (decimal) encoding 9633) for 0's showing up properly?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TógCúig
  • 1
  • 1
  • 3
    have you looked at how to use the following `Console.OutputEncoding = System.Text.Encoding.Unicode` `In .NET 4.5 and later also UTF-16 is supported ` – MethodMan Oct 15 '14 at 18:18
  • Besides the encoding, are you sure that the font used by the console has the white square glyph? – Chris Dunaway Oct 15 '14 at 21:08
  • It's look like your point about the font used by the console could be the problem. I came across a questions from somebody with a similar issue. It seems that if a character is not in the font used by the console it will output a "?" – TógCúig Oct 16 '14 at 00:36

0 Answers0