[英]How to convert this hex string to unicode in java?
I get below result from a web service: 我从Web服务获得以下结果:
"\\x52\\x50\\x1F\\x1F\\x44\\x46\\x57\\x47"
I need to get the strings in unicode characters, which i think would be: 我需要以unicode字符获取字符串,我认为这是:
"\u0052\u0050\u001F\u001F\u0044\u0046\u0057\u0047"
ie "RPDFWG" 即“ RPDFWG”
I cannot use replace("\\\\x", "\\u00\u0026quot;);
我不能使用
replace("\\\\x", "\\u00\u0026quot;);
because it says "\\u00\u0026quot; is not a valid unicode 因为它说“ \\ u00”不是有效的unicode
This code works for me: 该代码对我有用:
try {
String orig = "\\x52\\x50\\x1F\\x1F\\x44\\x46\\x57\\x47";
byte[] bytes = new byte[orig.length() / 4];
for (int i = 0; i < orig.length(); i += 4) {
bytes[i / 4] = (byte) Integer.parseInt(orig.substring(i + 2, i + 4), 16);
}
System.out.println(new String(bytes, "UTF-8"));
}
catch (Exception e) {
e.printStackTrace();
}
You might want to change the encoding to ISO-8859-1
, or just plain ASCII
-- I can't tell from your example what encoding is relevant here. 您可能希望将编码更改为
ISO-8859-1
,或者仅更改为纯ASCII
-我无法从您的示例中看出什么编码与此处相关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.