.NET用十六进制代码的字符串表示形式替换不可打印的ASCII [英] .NET replace non-printable ASCII with string representation of hex code
本文介绍了.NET用十六进制代码的字符串表示形式替换不可打印的ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,其中包含一些不可打印的ascii字符,例如:
I have a string with some non-printable ascii characters in it, something like:
"ABCD\x09\x05\r\n"
我想用ASCII字符串表示形式替换这些字符十六进制代码,所以我得到这样的东西:
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?
推荐答案
模式 \p {Cc}
匹配任何控制字符,因此
The pattern \p{Cc}
matches any control character, so
Regex.Replace(input,
@"\p{Cc}",
a=>string.Format("[{0:X2}]", (byte)a.Value[0])
);
也将替换控制字符。
这篇关于.NET用十六进制代码的字符串表示形式替换不可打印的ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文