0

I have to show unicode of input characters for example A -> 65 but what should I do for characters like emojis? -> 125814

meraat
  • 3
  • 1
  • 1
    `"".toCodePoints().forEach(System.out::println)` or `"".codePointAt(0)` or `Character.codePointAt("", 0)` – user85421 Feb 27 '20 at 19:55

1 Answers1

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