3

I am newbie in android/java. I'd like to show a human-readable string of a hex value in a CharSequence on a EditText. The code is here. This code shows the string representation of the hex e.g: 7465737420202020203f7465737420202020202020202020203f7465737420202020202020202020203fd3f7d3f7d3f77f078840ffffffffffff

This is what I want to show:

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

How should I do to convert hex value in text. I tried many convert ways but I either got runtime errors or a wrong characters.

PHA
  • 1,588
  • 5
  • 18
  • 37

1 Answers1

4

You can do like this:

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

This code takes in consideration that the original string will be always valid. There are no checkings.

It's the basic idea, hope it helps!

Laerte
  • 7,013
  • 3
  • 32
  • 50