Convert utf-8 encode in to a String in android

2019-07-15 09:22发布

问题:

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

回答1:

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..



回答2:

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.



回答3:

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