开发者

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

I have a string with some non-printable ascii characters in it, something like:

"AB开发者_JAVA技巧CD\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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜