Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how/unable to convert garbled/strange text to utf-8 android (java)?

I have garbled text è¼å¥ which is returned by web service (php) fetched from MySql

Now I am trying to decode it to utf-8 in android, but it's not working

I have tried:

String s = "è¼å¥";// text returned by web service taking it as static for testing

1. not working:

String str = new String(s.getBytes(), "utf-8");

2. not working:

String normalized = Normalizer.normalize(str, Normalizer.Form.NFD);
// also tried NFC, NFKC, NFKD
// also tested by isNormalized its returning true 

3. not working:

String str =URLDecoder.decode(s, "utf-8");

all above are giving same output: è¼å¥

So, please can anyone help me understand what I am doing wrong? Or please provide me any alternative?

Any help will be much appreciated. Thanks

like image 902
Tarsem Singh Avatar asked Dec 25 '22 22:12

Tarsem Singh


1 Answers

As Stephen C explained very well i followed all that steps, but little additional changes are required :

1. As Stephen C explained my server was sending data in Latin-1 encoding so i have to use ISO8859_1 charset

2. i was trying String str = new String(s.getBytes(), "utf-8");

this will not work for Latin-1 encoded data !

so for this i have to set the charset (for my case ISO8859_1) of data to getBytes(" ISO8859_1")

so this is working fine now

String str = new String(s.getBytes("ISO-8859-1"), "utf-8");

Note second parameter is for charset of new string so it must be utf-8 to display the original text

like image 149
Tarsem Singh Avatar answered Dec 31 '22 12:12

Tarsem Singh