Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 转换十六进制+;字符对字符_Java_Html_Dom_Escaping_Decode - Fatal编程技术网

Java 转换十六进制+;字符对字符

Java 转换十六进制+;字符对字符,java,html,dom,escaping,decode,Java,Html,Dom,Escaping,Decode,我想将这样一个包含十六进制代码和普通字符的字符串转换为Java中的字符。 示例输入为\x3e\x3c/style\x3e\x3cscript\x3e\x3c 如果字符串为,则输出应为 x = '\x3e\x3c/style\x3e\x3cscript\x3e\x3c!' 然后使用 x.decode('string_escape') 这里有一些东西可以试试。这不是一行代码,因为urldecker.decode想要抛出一个异常 import java.net.URLDecoder; publi

我想将这样一个包含十六进制代码和普通字符的字符串转换为Java中的字符。 示例输入为\x3e\x3c/style\x3e\x3cscript\x3e\x3c

如果字符串为,则输出应为

x = '\x3e\x3c/style\x3e\x3cscript\x3e\x3c!'
然后使用

x.decode('string_escape')

这里有一些东西可以试试。这不是一行代码,因为urldecker.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><!

>否字符串是\x3e\x3c/style\x3e\x3cscript\x3e\x3c!输出应该是>一行输入是“callbackProxy({”7103/usatody/high_impact/entertainment/main:{”type:“html”,“expandable:”false,“html:”\x3chtml\x3e\x3chead\x3e\x3cstyle\x3e\x3c!-\na:link{)。这里需要将\替换为\\和“with”\"在分配到字符串之前。现在这是一个不同的问题。请尝试将输入解析为字段。在StackOverflow上作为不同的问题提问,并显示您尝试过但不起作用的代码。非常感谢它起了作用。唯一剩下的是输入实际上是\x3e\x3c/style\x3e\x3cscript\x3e\x3c!。不是\\。因此它不是一个正确的字符串。我如何才能在将其作为输入时添加一个额外的\。如果从输入读取该字符串,则不需要添加额外的反斜杠。创建字符串常量需要额外的反斜杠,因为字符串的Java语法需要它。