Java: UTF-32 в UTF-16 преобразователь

Обновить

April 2019

Просмотры

669 раз

1

Я пытаюсь получить код Java бежать.
Пример : 1F612==>\ud83d\ude12

Я старался:

String toConvert = "\ud83d\ude12";
String result = "";
for(int x=0;x<toConvert.length();x++){
    int codePoint = Character.codePointAt(toConvert, x);
    String hexStr = Integer.toHexString(codePoint);
    hexStr = formatUTF(hexStr);
    result += hexStr;
}
System.out.println(result);

formatUTF функции:

public static String formatUTF(String hex){
    String text = hex;
    for(int x = 0; x<4-hex.length();x++)
        text = "0"+text;
    return "\\u"+text;
}

но выход:

run:
\u1f612\ude12

Примечание : 1F612Hex = 128530Integer

пожалуйста помоги.

1 ответы

1

Maybe this clarifies all.

    for (int i = 0; i < toConvert.length(); ) {
        int codePoint = Character.codePointAt(toConvert, i);
        i += Character.charCount(codePoint);
        System.out.printf("[%d] cp: %x%n", i, codePoint);
    }
    for (int i = 0; i < toConvert.length(); ++i) {
        char ch = toConvert.charAt(i);
        System.out.printf("[%d] c: %x%n", i, (int)ch);
    }

It yields one single code point of two 16 bit chars.

    [2] cp: 1f612
    [0] c: d83d
    [1] c: de12

Exactly as the UTF-16 standard says.