I have an application. 2 EditText, one is the text input field in utf, the second is the output of the converted text in the format "\u041f\u0440\u0438\u0432\u0435\u0442" I enter: "Привет" , and in response I should receive "\u041f\u0440\u0438\u0432\u0435\u0442"

I found such code on the Internet, but it did not work for me:

 String ucs = ed_utf.getText().toString(); //получаю входные данные try { byte[] utf8 = ucs.getBytes("UTF-8"); String unicodeData = new String(utf8, "UTF-8"); ed_unicode.setText(unicodeData); //передаю строку в EditText2 } catch (UnsupportedEncodingException e) { e.printStackTrace(); } 

The output is the same as in the input EditText.

    1 answer 1

    You can try this:

     private static String getUnicodeCodes(String str) { StringBuilder result = new StringBuilder(); for (int i = 0; i < str.length(); i++) result.append("\\u" + Integer.toHexString(str.charAt(i) | 0x10000).substring(1)); return result.toString(); } 

    The decision is borrowed from here.

    • Thank! A little changed for themselves, and earned. - De-Bill