2016-09-23 255 views
4

I 'm using following data to decode the UTF-8 encoded String.In Android, How to decode 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"); 

    } 
+0

I don't think your 'encodedString' is utf8 encoded – Gohan

+0

It is. You can decode it here: https://mothereff.in/utf-8 , and result would be fine. –

+0

Please post more code so it is reproducable for us. It's pretty unclear what you are doing now. For instance you should start with a string initialized and then 'String encodedString = .....;' and so on. – greenapps

回答

3

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"); 
+0

No gain, URLEncoder and String encoding has a difference. You can check at these links with same values, result would be different: https://mothereff.in/utf-8 http://www.freeformatter.com/url-encoder.html#ad-output –

2

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")); 
+0

isn't it the same as mine? –