ashu
ashu

Reputation: 1836

Why dec to string conversion not working correctly on android?

I am making an android app which parses XML. Since XML data contains HTML hexes (&#<dec value>), I need to convert them and then show them in my app. When I am passing 8217 to my code, it returns me some chinese/japanese (I'm not sure) character. Here is my code.

public char decToChar(String dec){
    Int decimal = Integer.parseInt(dec, 16);
    return (char)decimal;
}

I am passing value '8217' to this method and it returns chinese character instead of '.

Do anyone have any idea why its not working?

Upvotes: 1

Views: 168

Answers (2)

Tuna
Tuna

Reputation: 3005

You can use StringEscapeUtils.unescapeHtml4 to unescape an entity:

System.out.println(StringEscapeUtils.unescapeHtml4("&#8217;"));

Should show you the expected character

Upvotes: 1

Carsten Hoffmann
Carsten Hoffmann

Reputation: 941

what are you expecting? You are probably correctly parsing the hex-value to 33303. But since you are casting it to (char) you generate a meaningless value. This is not a conversion to a character.

I would advice you to use apache commons if you can. StringEscapeUtils will do the trick.

Upvotes: 1

Related Questions