Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 551
PHA Avatar asked Jan 25 '26 00:01

PHA


1 Answers

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!

like image 162
Laerte Avatar answered Jan 26 '26 13:01

Laerte