我想要将包含十六进制码和普通字符的字符串转换为Java中的字符。示例输入是\x3e\x3c/style\x3e\x3cscript\x3e\x3c!
输出应该是><style><script><!
发布于 2013-06-18 05:04:25
这里有一些可以尝试的东西。这不是一个单行代码,因为URLDecoder.decode想抛出一个异常。
import java.net.URLDecoder;
public class TestDecode {
public void run() throws Exception {
String test = "\\x3e\\x3c/style\\x3e\\x3cscript\\x3e\\x3c!";
System.out.printf("%s\n", URLDecoder.decode(test.replaceAll("\\\\x", "%"), "UTF-8"));
}
public static final void main(String[] args) throws Exception {
TestDecode td = new TestDecode();
td.run();
}
}运行时的输出为:
></style><script><!发布于 2013-06-18 04:39:31
如果字符串是
x = '\x3e\x3c/style\x3e\x3cscript\x3e\x3c!'然后使用
x.decode('string_escape')https://stackoverflow.com/questions/17156296
复制相似问题