Hex To String in Java Performance is too slow

Viewed 13615

I have the following method for my android program, written in Java.

The method takes in a string of hex and returns a string of the same text written in ascii.

public static String hexToString(String hex)
{
    StringBuilder sb = new StringBuilder();

    for (int count = 0; count < hex.length() - 1; count += 2)
    {
        String output = hex.substring(count, (count + 2));    //grab the hex in pairs

        int decimal = Integer.parseInt(output, 16);    //convert hex to decimal

        sb.append((char)decimal);    //convert the decimal to character
    }

    return sb.toString();
}

The method works fine, however my program is very time critical and this method is potentially being called tens of thousands of times. When analysing the slow bits of my program, this method takes up far too much time because of:

Integer.parseInt(output, 16);

and

hex.substring(count, (count + 2));

In the order of slowest first.

Does anyone know of a faster method of achieving the same thing?

4 Answers
Related