如何从十六进制编码的字符串解码为UTF-8字符串
How to decode to UTF-8 String from Hex encoded string
我得到HTML Javascript字符串,例如:
htmlString = "https'x3a'x2f'x2ftest.com"
但我想解码如下:
str = "https://test.com"
也就是说,我想要一个Util API,比如:
public static String decodeHex(String htmlString){
// do decode and converter here
}
public static void main(String ...args){
String htmlString = "https'x3a'x2f'x2ftest.com";
String str = decodeHex(htmlString);
// str should be "https://test.com"
}
有人知道如何实现这个API-decodeHex吗?
这应该足以让您开始。我把实现hexDecode
和整理格式错误的输入作为一个练习留给您。
public String decode(String encoded) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < encoded.length(); i++) {
if (encoded.charAt(i) == ''' && (i + 3) < encoded.length() && encoded.charAt(i + 1) == 'x') {
sb.append(hexDecode(encoded.substring(i + 2, i + 4)));
i += 3;
} else {
sb.append(encoded.charAt(i));
}
}
return sb.toString;
}
public String decode(String encoded) throws DecoderException {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < encoded.length(); i++) {
if (encoded.charAt(i) == '''' && (i + 3) < encoded.length() && encoded.charAt(i + 1) == 'x') {
sb.append(new String(Hex.decodeHex(encoded.substring(i + 2, i + 4).toCharArray()),StandardCharsets.UTF_8));
i += 3;
} else {
sb.append(encoded.charAt(i));
}
}
return sb.toString();
}
相关文章:
- Json字符串可以'当字符串末尾有“'时,t解码;
- 为什么我不必在 JS 中解码/解析 PHP 生成的 JSON 字符串
- 需要使用Java编码/转义HTML字符串,并在JavaScript中解码HTML字符串
- 用JavaScript对字符串进行编码/解码
- 如何在Javascript中对JSON字符串中的阿拉伯语进行编码/解码
- jQuery.parseJSON()未在Windows服务器上解码有效的Json字符串
- 用Ruby/Javascript解码带引号的可打印字符串
- 使用map函数对具有数字数组的字符串进行解码
- PHP 字符串正在将空格更改为 + 符号.需要解码以保留空格
- 编码/解码十六进制为 utf-8 字符串
- 如何在我的 AngularJS 应用程序中解码 URL 的部分查询字符串
- 如何从字符串解码链接
- 如何在 JavaScript 中对字符串进行编码并在 Java 中解码字符串
- 如何在java中解码由javascript使用readAsDataURL()生成并由Json发送的字符串
- 如何将中文十六进制字符串解码为中文字符或JavaScript
- Angular - 将字符串解码为 HTML
- JavaScript JSon字符串解码
- 在javascript中将字符串解码为数组值
- 为什么我得到JSON字符串解码错误时使用extjs ajax
- 如何从十六进制编码的字符串解码为UTF-8字符串