简体   繁体   中英

how to convert from decimal to ASCII characters

Im trying to write a function that will convert the last two Hex in a string to ASCII characters. like "ab3A" should print "ab:"

this is the code i wrote, it converts the last two to decimal but its unable to convert that decimal to ASCII characters. i tried to use .toString() to accomplish it, but no success.

 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;


}

You are very close: all you need is a cast instead of a call of 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;
}

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM