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?
CodePudding user response:
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 control codes above 0x80 so you're out of luck for those
CodePudding user response:
var stringBuilder = new StringBuilder();
foreach (var character in input)
{
if (character.IsControl())
stringBuilder.Append(Convert.ToInt32(character).ToString("X4"));
}
return stringBuilder.ToString();