Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

You can use StringEscapeUtils.unescapeHtml4 to unescape an entity:

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

Should show you the expected character

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top