0

I received an unicode string to contain Emoji code, example: "U+1F44F" (from Emoji table : http://apps.timwhitlock.info/emoji/tables/unicode).

I want to convert this string to an Integer how can I do that ?

I tried this, but it crashs:

int hex = Integer.parseInt(unicodeStr, 16);

Thanks guys!

2
  • My guess is that you can drop the leading U+ and convert the 1F44F from hex to decimal. Commented Apr 19, 2016 at 19:47
  • int hex = Integer.parseInt(unicodeStr.substring(2), 16); Commented Apr 19, 2016 at 20:02

2 Answers 2

4

The comment of @flakes gives the correct answere. The U+ only indicates that the following codepoint (or hex number) is a Unicode. The value you want to convert into an Integer is the codepoint, so you have to omit the 2 first characters with .substring(2)

You wil obtain the following code:

int hex = Integer.parseInt(unicodeStr.substring(2), 16);
Sign up to request clarification or add additional context in comments.

Comments

1

Unicode numbers such "characters," code points, upto the 3 byte range, such as U+1F44F.

Java String has a constructor with code points.

int[] codepoints = { 0x1F44F };
String s = new String(codepoints, 0, codepoints.length);

public static String fromCodepoints(int... codepoints) {
    return new String(codepoints, 0, codepoints.length);
}

s = fromCodepoints(0x1F44F, 0x102);

Java String contains Unicode as an internal array of chars. Every char '(2 bytes) being UTF-16 encoded. For lower ranges a char can be a code point. And U+0102 could be written as "\u0102" containing the char '\u0102'.

Note that emoji must be representable in the font.

Font font = ...
if (!font.canDisplay(0x1F44F)) {
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.