5

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

    } 
6
  • I don't think your encodedString is utf8 encoded Commented Sep 23, 2016 at 11:55
  • It is. You can decode it here: mothereff.in/utf-8 , and result would be fine. Commented Sep 23, 2016 at 11:59
  • 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. Commented Sep 23, 2016 at 12:02
  • its simple one line method, please check Commented Sep 23, 2016 at 12:05
  • There is nothing to check if you do not provide reproducable code. We cannot even see how and with what you call that function. We do not know what that parameter encodedString contains. So show full code. Commented Sep 23, 2016 at 12:06

2 Answers 2

5

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");
Sign up to request clarification or add additional context in comments.

1 Comment

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

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

1 Comment

isn't it the same as mine?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.