Convert utf-8 encode in to a String in android

2019-07-15 09:18发布

i have json value Like

\u092a\u093e\u0932\u094d\u092a\u093e\u0915\u093e \u092c\u0928\u094d\u0926\u0940\u0939\u0930\u0942 \u0915\u093e\u0930\u093e\u0917\u093e\u0930\u092d\u093f\u0924\u094d\u0930\u0948 \u0905\u0938\u0941\u0930\u0915

How we get String or how to decode it in android and display it on text view.

i m perform some opration on it but it show log ??????????????????????????????

can some one help Me. Thanks

3条回答
在下西门庆
2楼-- · 2019-07-15 09:24

K Thanks friends for reply all ur given code works on Real device but it doesn't work on emulator

we don't have to display text on real device just set text in your textview and it works fine.

tv1.setText(str);

查看更多
ら.Afraid
3楼-- · 2019-07-15 09:39

the problem is that you are converting your encrypted data to a String. encrypted data is binary, not String data. UTF-8 is a charset with a specific encoding format. arbitrary binary data is not valid UTF-8 data. when you convert the encrypted data into a String, the "invalid" characters are most likely getting replaced with the ? invalid char.

If you want to convert arbitrary binary data (aka encrypted data) into a String, you need to use some binary->text conversion like Base64.

查看更多
Emotional °昔
4楼-- · 2019-07-15 09:46

From ref from here . You only need new String(bytes, charset) and String.getBytes(charset)..

Try following code

 String data = "\u092a\u093e\u0932\u094d\u092a\u093e\u0915\u093e \u092c\u0928\u094d\u0926\u0940\u0939\u0930\u0942 \u0915\u093e\u0930\u093e\u0917\u093e\u0930\u092d\u093f\u0924\u094d\u0930\u0948 \u0905\u0938\u0941\u0930\u0915";
            byte[] bute = null;
            bute = data.getBytes();
            try {
                String asd= new String(bute, "UTF-8");

                System.out.println(asd);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

I am sure it will print the data you want...

Hope this helps..

查看更多
登录 后发表回答