2

我试图编写一个函数,将字符串中的最后两个十六进制转换为 ASCII 字符。像“ab3A”应该打印“ab:”

这是我写的代码,它将最后两个转换为十进制,但无法将该十进制转换为 ASCII 字符。我尝试使用 .toString() 来完成它,但没有成功。

 private static String unmangle(String word)
 {      
    String newTemp = word.substring(word.indexOf('%')+1);
    int hex = hexToInt(newTemp);
    String strI = Integer.toString(hex);

    System.out.println(strI);

    word=word.replace("%", "");
    word=word.replace("+", " ");
    return word = word.replace(newTemp, "")+ strI;


}
4

2 回答 2

3

你非常接近:你所需要的只是一个演员而不是一个电话Integer.toString-

private static String unmangle(String word)
{      
    String newTemp = word.substring(word.indexOf('%')+1);
    char hex = (char)hexToInt(newTemp);

    word=word.replace("%", "");
    word=word.replace("+", " ");
    return word = word.replace(newTemp, "")+ hex;
}
于 2013-08-26T00:39:10.513 回答
0

First you should know how to make Hex to ASCII.

String newTemp=word.substring(word.length-1,word.length);
char a=newTemp.substring(0,0);
char b=newTemp.substring(1,1);
int c=0,d=0;
//And you should convert to int.
if(a='A'|'a')
c=10;//the same to d.
//and 
c=c*16+d;
String strI = Integer.toString(c);
return word.substring(0,word.length-2)+strI;

I am so sorry my English is not well. And this is my way to deal with this question.String strI = Integer.toString(hex);

this sentence is wrong.It makes hex to string.For example if hex=97,StrI="97" not "a"

于 2013-08-26T01:03:57.730 回答