Search code examples
javaandroidstringunicodeunicode-string

Java convert String to Unicode character. "U+1F600" = 😀


Please note that this question is not a duplicate.

I have a String like this:

// My String
String myString = "U+1F600";
// A method to convert the String to a real character
String unicodeCharacter = convertStringToUnicode(myString);
// Then this should print: 😀
System.out.println(unicodeCharacter);

How can I convert this String to the unicode character 😀? I then want to show this in a TextView.


Solution

  • What you are trying to do is to print the unicode when you know the code but as String... the normal way to do this is using the method

    Character.toChars(int)

    like:

    System.out.print(Character.toChars(0x1f600));
    

    now in you case, you have

    String myString = "U+1F600";
    

    so you can truncate the string removing the 1st 2 chars, and then parsing the rest as an integer with the method Integer.parseInt(valAsString, radix)

    Example:

    String myString = "U+1F600";
    System.out.print(Character.toChars(Integer.parseInt(myString.substring(2), 16)));