我的 android 程序有以下方法,用 Java 编写。
该方法接受一个十六进制字符串并返回一个用 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();
}
该方法工作正常,但是我的程序对时间非常关键,并且该方法可能被调用数万次。在分析我的程序的慢位时,这种方法占用了太多时间,因为:
Integer.parseInt(output, 16);
和
hex.substring(count, (count + 2));
按照最慢的顺序。
有谁知道实现相同目标的更快方法?