Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Unicode to escaped Unicode programmatically

I need to figure out the way to convert a Unicode value to escaped code. For example, convert 0x1f604 to "\uD83D\uDE04".

like image 708
Sami Issa Avatar asked Oct 21 '25 17:10

Sami Issa


1 Answers

It seems you're looking for an escaping that first converts a Unicode codepoint (32-bit integer value) to UTF-16 encoding (multiple 16-bit values), which is the encoding Java uses internally for strings.

Then each 16-bit value uses an escaping syntax as in Java or Javascript.

public static String encodeCodepoint(int codePoint) {

    char[] chars = Character.toChars(codePoint);
    StringBuilder sb = new StringBuilder();
    for (char ch : chars) {
        sb.append(String.format("\\u%04X", (int)ch));
    }
    return sb.toString();
}

The following code:

System.out.println(encodeCodepoint(0x1f604));

outputs:

\uD83D\uDE04
like image 176
Codo Avatar answered Oct 24 '25 08:10

Codo



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!