user1896464
user1896464

Reputation: 345

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;


}

Upvotes: 2

Views: 7959

Answers (2)

PengWu
PengWu

Reputation: 65

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"

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727067

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;
}

Upvotes: 3

Related Questions