1

I got

 T�vez & Messi: Argentine showdown

when I decoded a string using:

URLDecoder.decode("Tévez & Messi: Argentine showdown","UTF-8");

Why I am getting such a non-UTF-8 string.

Mubin Shrestha
  • 398
  • 3
  • 22
  • 1
    AFAIK if you use it with a string literal like that, the literal is stored in UTF-16 format in memory, so it tries to decode the UTF-16 as if it were UTF-8. – Mr Lister Jun 08 '15 at 18:26
  • @Mr Lister. Thank you. But I didn't get you. Whats the solution then. I tried with URLDecoder.decode("string", UTF-16) but the result still the same. – Mubin Shrestha Jun 08 '15 at 18:32
  • Make sure that your source code file is also stored as UTF-8. That may be the problem. –  Jun 08 '15 at 18:40
  • 1
    Also, you can try using the Unicode code for "é" itself: `URLDecoder.decode("T\u00E9vez & Messi: Argentine showdown","UTF-8");` (note the `\u00E9`). –  Jun 08 '15 at 18:43
  • @dudeprgm, Thank you. For the first time in my programming life I am hearing to save code as UTF-8. Interesting. I am using Android-Studio 1.2.11. How can I check that my IDE is storing source code as saving UTF-8. Also regarding unicode, I am expecting a lot of universal such literals. That probably would be bad idea. – Mubin Shrestha Jun 08 '15 at 18:46
  • 1
    @Computergodzilla Not sure about Android Studio, as I haven't used it. However, I did find this blog post about IntelliJ IDEA (Android Studio is derive from IntelliJ IDEA): http://blog.jetbrains.com/idea/2013/03/use-the-utf-8-luke-file-encodings-in-intellij-idea/. Scroll down to the part where it says "Check if you have specified the encoding explicitly and use that...", and try changing the default encoding of your source code file to "UTF-8". That may help if the source code file encoding is indeed the problem. –  Jun 08 '15 at 18:51
  • @Computergodzilla You can also look at this SO answer about changing file encodings to UTF-8: http://stackoverflow.com/a/24568901/837703. –  Jun 08 '15 at 18:53
  • @dudeprgm: Thank you. The default file encoding of Android-Studio 1.2.1.1 – Mubin Shrestha Jun 08 '15 at 18:55
  • @dudeprgm. Thank you. Android Studio default file encoding is UTF-8. Problem is still ON. – Mubin Shrestha Jun 08 '15 at 18:56
  • From where are you getting that text to decode? It's hardcoded? from a file? seems like you are trying to decode text with another charset. – vzamanillo Jun 08 '15 at 20:40
  • I think @vzamanillo is right, this happens if the source sends an iso-8859-1 String, might have to convert it on the source before sending. – maraca Jun 09 '15 at 02:28
  • @vzamanillo, Thank you. I have a web crawler that crawls websites. I get that string when crawling. Any my crawler is configured to UTF-8. – Mubin Shrestha Jun 09 '15 at 06:02

0 Answers0