将十六进制字节数组的字符串表示形式转换为 Java 中具有非 ascii 字符的字符串

Convert string representation of a hexadecimal byte array to a string with non ascii characters in Java

客户端在请求负载中发送了一个字符串:

"[0xc3][0xa1][0xc3][0xa9][0xc3][0xad][0xc3][0xb3][0xc3][0xba][0xc3][0x81][0xc3][0x89][0xc3][0x8d][0xc3][0x93][0xc3][0x9a]Departms"

我想得到一个字符串,它是 "áéíóúÁÉÍÓÚDepartms"。我如何在 Java 中执行此操作?

问题是我无法控制客户端编码此字符串的方式。似乎客户端只是以这种格式对非 ascii 字符进行编码,并按原样发送 ascii 字符(请参阅最后的 'Departms')。

方括号里面的东西,好像是用UTF-8编码的字符,但是以一种奇怪的方式转换成了十六进制字符串。你可以做的是找到每个看起来像 [0xc3] 的实例并将其转换为相应的字节,然后从字节中创建一个新的字符串。

遗憾的是,没有用于处理字节数组的好工具。这是一个快速而肮脏的解决方案,它使用正则表达式查找这些十六进制代码并将其替换为 latin-1 中的相应字符,然后通过重新解释字节来修复它。

String bracketDecode(String str) {
    Pattern p = Pattern.compile("\[(0x[0-9a-f]{2})\]");
    Matcher m = p.matcher(str);
    StringBuilder sb = new StringBuilder();
    while (m.find()) {
        String group = m.group(1);
        Integer decode = Integer.decode(group);
        // assume latin-1 encoding
        m.appendReplacement(sb, Character.toString(decode));
    }
    m.appendTail(sb);
    // oh no, latin1 is not correct! re-interpret bytes in utf-8
    byte[] bytes = sb.toString().getBytes(StandardCharsets.ISO_8859_1);
    return new String(bytes, StandardCharsets.UTF_8);
}