0

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?

Brondahl
  • 7,402
  • 5
  • 45
  • 74

2 Answers2

4

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

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Hilariously ... Notepad++ doesn't have a renderer for those characters :D thye just show up as squares :D – Brondahl Nov 10 '22 at 13:52
  • @Brondahl that's a font problem and has nothing to do with Notepad++. Choose a different font with those characters. Notepad++ doesn't have very good font substitution/font replacement like many other editors – phuclv Nov 10 '22 at 14:19
1
var stringBuilder = new StringBuilder();
foreach (var character in input)
  {
   if (character.IsControl())
     stringBuilder.Append(Convert.ToInt32(character).ToString("X4"));
  }

 return stringBuilder.ToString();
Lucian
  • 3,407
  • 2
  • 21
  • 18