溫馨提示×

java字符串怎么轉unicode編碼

小億
395
2023-12-14 14:44:11
欄目: 編程語言

可以使用String類的getBytes方法將字符串轉換為字節數組,然后再將字節數組轉換為Unicode編碼。下面是一個示例代碼:

public class Main {
    public static void main(String[] args) {
        String str = "Hello World!";
        
        // 將字符串轉換為字節數組
        byte[] bytes = str.getBytes();
        
        // 將字節數組轉換為Unicode編碼
        StringBuilder unicode = new StringBuilder();
        for (byte b : bytes) {
            unicode.append("\\u").append(Integer.toHexString(b & 0xFF));
        }
        
        System.out.println(unicode.toString());
    }
}

輸出結果為:\u48\u65\u6c\u6c\u6f\u20\u57\u6f\u72\u6c\u64\u21

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女