Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print control characters in Console Window

Tags:

c#

I have tab characters assigned in ASCII (09) or Unicode

char ch = '\x09';
(or)
char ch = '\u0009';

How do I print '\t' in console window ?

None of the below works.(maybe it prints a tab, but not as a canonical representation of '\t')

Console.Write(ch);
Console.Write(ch.ToString())

Guess,Console.Write() is not the right way to do it

like image 442
Antony Thomas Avatar asked Jan 28 '26 23:01

Antony Thomas


1 Answers

Control characters don't display, since that is the whole point of a control character. The reason for putting a tab in a piece of text is to have a tab, after all.

Ideally, we could use the standard symbols ␀␁␂␃␄␅␆␇␈␉␊␋␌␍␎␏␐␑␒␓␔␕␖␗␘␙␚␛␜␝␞␟␠␡␤␥␦ but they don't have great font support (right now as I see it, the symbol for Delete Form Two and Subsitute Form Two aren't showing correctly) and this is even worse with the console.

You're also not clear in your question whether you want the canonical representation (U+0009) or the C# escape (\t) as you ask for one right after asking for the other ("a canonical representation of '\t'").

Assuming the latter, we've an issue in that C# only provides such short-cut escapes for 8 of the control characters. The process will also requires us to escape \ for the same reasons that C# does - how otherwise can we detect whether \t means tab, or means \ followed by t?

So, assuming you want a form that you could then use directly in C# again, we can do the following:

public static class StringEscaper
{
    public static string EscapeForCSharp(this string str)
    {
        StringBuilder sb = new StringBuilder();
        foreach(char c in str)
            switch(c)
            {
                case '\'': case '"': case '\\':
                    sb.Append(c.EscapeForCSharp());
                    break;
                default:
                    if(char.IsControl(c))
                        sb.Append(c.EscapeForCSharp());
                    else
                        sb.Append(c);
                    break;
            }
        return sb.ToString();
    }
    public static string EscapeForCSharp(this char chr)
    {
        switch(chr)
        {//first catch the special cases with C# shortcut escapes.
            case '\'':
                return @"\'";
            case '"':
                return "\\\"";
            case '\\':
                return @"\\";
            case '\0':
                return @"\0";
            case '\a':
                return @"\a";
            case '\b':
                return @"\b";
            case '\f':
                return @"\f";
            case '\n':
                return @"\n";
            case '\r':
                return @"\r";
            case '\t':
                return @"\t";
            case '\v':
                return @"\v";
            default:
                //we need to escape surrogates with they're single chars,
                //but in strings we can just use the character they produce.
                if(char.IsControl(chr) || char.IsHighSurrogate(chr) || char.IsLowSurrogate(chr))
                    return @"\u" + ((int)chr).ToString("X4");
                else
                    return new string(chr, 1);
        }
    }
}

Now we can test it with both a string and a single char.

Single char:

Console.WriteLine('\t'.EscapeForCSharp());

Outputs:

\t

String:

string str = "The following string contains all the \"C0\" and \"C1\" controls, escaped with \\ as per C# syntax: "
  + "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0009\u000A\u000B\u000C\u000D\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F\u007F\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008D\u008E\u008F\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F";
Console.WriteLine(str.EscapeForCSharp());

Outputs:

The following string contains all the \"C0\" and \"C1\" controls, escaped with \\ as per C# syntax: \0\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n\v\f\r\u000E\u000F\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017\u0018\u0019\u001A\u001B\u001C\u001D\u001E\u001F\u007F\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089\u008A\u008B\u008C\u008D\u008E\u008F\u0090\u0091\u0092\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F
like image 196
Jon Hanna Avatar answered Jan 30 '26 11:01

Jon Hanna