Java 和 Kotlin 中,字符转Unicode,Unicode 转10进制

java:

public void test() {
    
    
    char charSymbol = '厂';
    String unicode = Integer.toHexString(charSymbol); // => 16 进制
    System.out.println("\\u" + unicode);
    System.out.println(Integer.parseInt(unicode, 16)); // => 10进制
}

kotlin:

fun test() {
    
    
    val charSymbol = '厂' // '\u5382'
    println(charSymbol.code) // <==> charSymbol.toInt(); => 10进制数
    println("\\u${
      
      charSymbol.code.toString(16)}") // 转16进制 => 5382, 就是 unicode 的表示值

    println("-----")

    // kotlin 调用 java api 实现
    val unicode = Integer.toHexString(charSymbol.code)
    println("\\u$unicode")
    println(Integer.parseInt(unicode, 16)) // => 10进制
}

猜你喜欢

转载自blog.csdn.net/jjwwmlp456/article/details/128946682