I have to show unicode of input characters for example A -> 65 but what should I do for characters like emojis? -> 125814
Asked
Active
Viewed 67 times
1 Answers
1
Here's a way to output the codepoint of any character:
import java.io.UnsupportedEncodingException;
public class Test
{
public static int getCodePoint(String s) throws UnsupportedEncodingException
{
byte[] a = s.getBytes("UTF-32BE");
return ((a[1] & 0xFF) << 16) + ((a[2] & 0xFF) << 8) + (a[3] & 0xFF);
}
public static void main(String []args) throws UnsupportedEncodingException
{
System.out.println(getCodePoint("A")); // 65
System.out.println(getCodePoint("")); // 128514
}
}

Olivier
- 13,283
- 1
- 8
- 24
-
While this may work, it is not a good solution since Java already provides `Character.codePointAt()` and `String.codePointAt()`, as mentioned in a comment to the question. – skomisa Mar 01 '20 at 07:24
-
@skomisa You're right, I thought that codePointAt() was not supporting surrogate pairs, but it turns out it does. – Olivier Mar 01 '20 at 11:16