Jeetu
Jeetu

Reputation: 676

Convert utf-8 encode in to a String in android

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

Upvotes: 2

Views: 10263

Answers (3)

CRUSADER
CRUSADER

Reputation: 5472

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

Upvotes: 7

Jeetu
Jeetu

Reputation: 676

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

Upvotes: -2

No_Rulz
No_Rulz

Reputation: 2719

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.

Upvotes: 1

Related Questions