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

13,627

Solution 1

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

Solution 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"));
Share:
13,627
Usman Rana
Author by

Usman Rana

Updated on June 15, 2022

Comments

  • Usman Rana
    Usman Rana almost 2 years

    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");
    
        }