Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 735
Thomas Vos Avatar asked Sep 03 '26 14:09

Thomas Vos


1 Answers

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)));
like image 173


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!