Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android, How to decode UTF-8 encoded String?

I 'm using following data to decode the UTF-8 encoded String.

  • Actual string: 秦世俊:用“心”工作 找到属于自己的成就感【开讲啦 20160430】T

  • UTF-8 encoded: 秦ä¸ä¿ï¼ç¨âå¿âå·¥ä½ æ¾å°å±äºèªå·±çæå°±æãå¼è®²å¦ 20160430ã"

. Output is same as input. What is the issue?

Method:

public String decodeString(String encodedString) {
            return new String(encodedString.getBytes(), "UTF-8");

    } 
like image 639
Usman Rana Avatar asked Sep 23 '16 11:09

Usman Rana


2 Answers

Just Use String result = URLDecoder.decode(string, "UTF-8");

Or use this

byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
like image 136
Preetika Kaur Avatar answered Oct 12 '22 10:10

Preetika Kaur


use this

String s2 = new String(bytes, "UTF-8"); // Charset with which bytes were encoded 

and if it doesn't work for you

String decoded = new String(encoded.getBytes("ISO-8859-1"));
like image 31
Rahul Khurana Avatar answered Oct 12 '22 11:10

Rahul Khurana