I have a string with some non-printable ascii characters in it, something like:
"ABCD\x09\x05\r\n"
I want to replace these characters with a ascii string representation of the hex code numbers, so I get something like this:
"ABCD[09][05][0D][0A]"
Whats the best way to do this? Can a regex be used?
The pattern \p{Cc} matches any control character, so
Regex.Replace(input,
@"\p{Cc}",
a=>string.Format("[{0:X2}]", (byte)a.Value[0])
);
would also replace control characters.
string s = "ABCD\x09\x05\r\n";
var replace =
s.Select(c => Char.IsControl(c) ? ((int)c).ToString("X2") : c.ToString())
.Aggregate(new StringBuilder(), (sb, t) => sb.Append(t))
.ToString();
Sorry, no compiler handy, but I think this compiles and does the job.
Also, this kind of walks the string twice (once to project each character to a hex replacement or a string, and then again to aggregate), and you can avoid this by lumping the projection into the call to Enumerable.Aggregate, but this is more clear and it probably doesn't matter that much unless this is performance-critical.
Inspired by Jason's example, but a bit simpler. I'm not sure which performs better, and don't have the time to benchmark it right now, but it should do everything in just one pass:
string s = "ABCD\x09\x05\r\n";
string replace = String.Concat(s.Select(c => Char.IsControl(c) ?
String.Format("[{0:X2}]", (int)c) :
c.ToString()));
I've tested this for functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With