如何将带有十六进制字符的字符串转换为该十六进制的人类可读值

How convert string with hex characters convert to human readable value of that hex

我是 android/java 的新手。我想在 EditText 的 CharSequence 中显示人类可读的十六进制值字符串。 The code is here。此代码显示十六进制的字符串表示形式,例如:7465737420202020203f7465737420202020202020202020203f7465737420202020202020202020203fd3f7d3f7d3f77f078840ffffffffffff

这是我要展示的:

test     ?test           ?test           ?Ó÷Ó÷Ó÷ˆ@ÿÿÿÿÿÿ

如何转换文本中的十六进制值。我尝试了很多转换方式,但要么出现运行时错误,要么出现错误字符。

你可以这样做:

    String a = " 7465737420202020203f7465737420202020202020202020203f7465737420202020202020202020203fd3f7d3f7d3f77f078840ffffffffffff";
    a = a.trim();
    StringBuffer result = new StringBuffer();

    for (int i = 0; i < a.length(); i+=2) {
        String sub = a.substring(i, i+2);
        int b = Integer.parseInt(sub, 16);
        result.append((char)b);
    }

    System.out.println(result);

此代码考虑到原始字符串将始终有效。没有检查。

基本思路,希望对你有帮助!