Search code examples
c#stringcontrol-characters

Does C# have built-in methods to convert string control characters to "normal characters"?


There is a Char.IsControl() method to identify such characters, but I want a way to convert them to "normal" characters. i.e. some way to visualise a string that contains such characters. Probably similar to what Notepad++ does.

Obviously such a visualisation will be imperfect, and ambiguous ... but does it exist as a built in method?


Solution

  • Notepad++ is converting the control characters to the control pictures U+240x and you'll also have to do that yourself. To convert controlChar to its visualization just use controlChar + 0x2400 or controlChar + '\u2400'

    Console.Write((char)(i + 0x2400));
    

    Demo on dotnetfiddle. Output:

    ␀ ␁ ␂ ␃ ␄ ␅ ␆ ␇ ␈ ␉ ␊ ␋ ␌ ␍ ␎ ␏ ␐ ␑ ␒ ␓ ␔ ␕ ␖ ␗ ␘ ␙ ␚ ␛ ␜ ␝ ␞ ␟
    

    You can also do like this

    Console.Write(char.ConvertFromUtf32(i + '\u2400'));
    

    Of course this only works for the first 32 control characters (and space), to convert the remaining ones you have to use a Dictionary or something similar because the code points aren't contiguous. For example 0x7F will need to be converted to ␡ (U+2421). There aren't any control pictures for the C1 control codes so you're out of luck for those. There are many other control codes above U+0080 and most of them don't have a control picture either