6

i want convert a simple text like, "my simple text" to Unicode character in Android.

for example this :

"\u0628\u0631\u0646\u0627\u0645\u0647 \u0646\u0648\u06cc\u0633\u06cc"

this string in uni code is :

برنامه نویسی

i want input

برنامه نویسی

and response

"\u0628\u0631\u0646\u0627\u0645\u0647 \u0646\u0648\u06cc\u0633\u06cc"

this string . please help me .

Paolo Zaia
  • 218
  • 3
  • 14
mGol
  • 106
  • 1
  • 1
  • 9
  • Have you already tried anything? – Yassin Hajaj Nov 23 '15 at 13:13
  • You are mixing a lot of concepts here... What is the initial content of your text file? What encoding does it use? – fge Nov 23 '15 at 13:13
  • are you getting those string from database or edittext? – Kostas Drak Nov 23 '15 at 13:15
  • Check Oracle [StringConverter](https://docs.oracle.com/javase/tutorial/i18n/text/examples/StringConverter.java) and [UnicodeFormatter](https://docs.oracle.com/javase/tutorial/i18n/text/examples/UnicodeFormatter.java) – Jordi Castilla Nov 23 '15 at 13:16
  • what is wrong to just return the `byte[]` representation of the input? – nafas Nov 23 '15 at 13:33
  • I would not say that it is duplicate of http://stackoverflow.com/questions/11145681/how-to-convert-a-string-with-unicode-encoding-to-a-string-of-letters .. In real it is inverse of that defect, OP wants to achieve opposite of what is talked in that defect .. Now, arguing that do reverse of that is not a rational argument .. – hagrawal7777 Nov 23 '15 at 14:19
  • Yassin Hajaj ; yes i do anything. helldawg13 ; use Edittext. nafas ; how ?! hagrawal; my question was different. – mGol Nov 24 '15 at 06:32

2 Answers2

7

Android native its java, so.. check this:

You can do it for any Java char using the one liner here:

System.out.println( "\\u" + Integer.toHexString('÷' | 0x10000).substring(1) );

Reference: Get unicode value of a character

I hope that I have helped answer some of your questions.

Community
  • 1
  • 1
Javierif
  • 632
  • 1
  • 6
  • 18
0

Use Apache Commons Lang api. StringEscapeUtils.escapeJava() can help you get the answer.

import org.apache.commons.lang3.StringEscapeUtils;

public class StringUnicode {

    public static void main(String[] args) {

        String foreignText = "برنامه نویسی";

        String response = StringEscapeUtils.escapeJava(foreignText);
        System.out.println(response);
    }
}
Abhash Upadhyaya
  • 717
  • 14
  • 34