1

In my android app my server returns a UTF-8 encoded response like this; \u00e2\u0080\u0098 rank \u00e2\u0080\u0099 which is equivalent 'rank'. Kindly help me in converting this Unicode characters to corresponding symbols i.e

1.\u00e2\u0080\u0098 -> ' (LEFT SINGLE QUOTATION)

2.\u00e2\u0080\u0099 -> ' (RIGHT SINGLE QUOTATION)

Mehrdad
  • 708
  • 1
  • 17
  • 38
Agarathi
  • 148
  • 1
  • 14
  • I tried the following code String str = StringEscapeUtils.unescapeJava(str); – Agarathi Dec 28 '13 at 11:05
  • 1
    I don't think you mean `\u00e2\u0080\u0098`. I *hope* you mean the bytes 0xe2, 0x80, 0x98... but it's very unclear from your question. It doesn't help that you haven't shown any code - we don't know anything about how you're reading from your server. – Jon Skeet Dec 28 '13 at 11:07
  • @GareginSargsyan i tried the same but it is not working. I added the lang.jar to my build path and tried it but its not helping me. I saved the response from the server in a string (str) and then called str = StringEscapeUtils.unescapeJava(str); but it not decoding it. – Agarathi Dec 28 '13 at 11:09
  • @Jon Skeet I get JSON response from server and printed the same in the log it displays \u00e2\u0080\u0098. – Agarathi Dec 28 '13 at 11:10
  • 1
    @NaveenPrabhu: That seems pretty broken to me. U+0098 is "start of string" and U+0080 is a control character. Is this your server? If so, I'd start there and try to fix that first... – Jon Skeet Dec 28 '13 at 11:11
  • +1 what Jon said. `"\u00e2\u0080\u0098"` is *not* an encoding of U+2018 Left Single Quotation Mark and any interface that is giving you that is in error. Whilst you *could* attempt to unmangle the string by encoding to cp1252 and decoding with UTF-8, the real problem you should be trying to fix is in the code that produced this output. By the way if you are dealing with JSON strings then you should be using a real JSON decoder which will deal with `\u` escapes automatically, and *not* `unescapeJava` - the rules of Java string literals are different to those of JSON. – bobince Dec 28 '13 at 22:49

1 Answers1

0

This is the way, replace "some text" with ur string -

   String s1 = "some text";
        String s2 = "";
        byte[] bytes;
        try {
            bytes = s1.getBytes("UTF-8");
             s2 = new String(bytes, "UTF-8"); // Charset with which bytes were encoded
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

s2 have required string.

keshav
  • 3,235
  • 1
  • 16
  • 22
  • You are encoding a string to bytes using UTF-8 and then decoding the bytes back to string with the exact same encoding. This code has no effect at all. – bobince Dec 28 '13 at 22:45