5

Using Java on Android I'm struggling to convert a couple of html special characters.

So far I've tried:

String myString = "%A32.00%20per%20month%B3";

Html.fromHtml(myString).toString(); => %A32.00%20per%20month%B3
URLDecoder.decode(myString) => �2.00 per month�
URLDecoder.decode(myString, "UTF-8") => �2.00 per month�
URLDecoder.decode(myString, "ASCII") => �2.00 per month�
org.apache.commons.lang.StringEscapeUtils.unescapeHtml4(myString) => %A32.00%20per%20month%B3

The correct output should be => £2.00 per month³

scottyab
  • 23,621
  • 16
  • 94
  • 105
  • 1
    Are you sure `URLDecoder.decode(myString, "UTF-8")` doesn't do the correct decoding but your output just can't display the `£` and `³` characters? – Thomas Dec 20 '11 at 16:56
  • Apache commons StringEscapeUtils escapes/unescapes Html, which is not what you need here. Eg. £2.00 per month³ <-> £2.00 per month³ – proko Dec 20 '11 at 17:16

2 Answers2

8

Your string is encoded in ISO-8859-1, so ASCII and UTF-8 won't work.

String myString = "%A32.00%20per%20month%B3";
URLDecoder.decode(myString, "ISO-8859-1");
// output: £2.00 per month³
Floern
  • 33,559
  • 24
  • 104
  • 119
2
public static void main(String[] args) throws UnsupportedEncodingException {
    String before = "£2.00 per month³";
    String encoded = URLEncoder.encode(before, "UTF-8");
    String decoded = URLDecoder.decode(encoded, "UTF-8");
    System.out.println(encoded);
    System.out.println(decoded);
}

In output I get:

%C2%A32.00+per+month%C2%B3
£2.00 per month³

Are you sure that %A32.00%20per%20month%B3 is correct?

proko
  • 1,180
  • 1
  • 7
  • 14
pawelzieba
  • 16,082
  • 3
  • 46
  • 72
  • Spot on it was the input string (from a 3rd party webservice) that seems to have incorrectly encoded the string. When you add %C2 infront of the symbols it works correctly. Thanks – scottyab Dec 20 '11 at 17:33