Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET replace non-printable ASCII with string representation of hex code

Tags:

string

c#

regex

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?

like image 597
user380689 Avatar asked Aug 12 '11 01:08

user380689


3 Answers

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.

like image 65
drf Avatar answered Nov 16 '22 04:11

drf


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.

like image 35
jason Avatar answered Nov 16 '22 05:11

jason


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.

like image 4
Frank Szczerba Avatar answered Nov 16 '22 06:11

Frank Szczerba