【问题标题】:How convert string with hex characters convert to human readable value of that hex [closed]如何将带有十六进制字符的字符串转换为该十六进制的人类可读值[关闭]
【发布时间】:2015-05-28 06:57:16
【问题描述】:

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

这就是我想要展示的:

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

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

【问题讨论】:

标签: java android android-intent android-activity


【解决方案1】:

你可以这样做:

    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);

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

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

【讨论】:

  • 使用 StringBuffer 代替?
  • 好建议。我编辑了帖子。
猜你喜欢
  • 2018-01-22
  • 2019-07-27
  • 2018-01-31
  • 1970-01-01
  • 2013-02-07
  • 2017-08-23
  • 2014-03-19
  • 2017-03-11
相关资源
最近更新 更多