我正在寻找一个类似于Kotlin中的JavaInteger.toHexString()
的函数.有没有内置的东西,或者我们必须手动编写一个函数来将Int
转换为String
?
我正在寻找一个类似于Kotlin中的JavaInteger.toHexString()
的函数.有没有内置的东西,或者我们必须手动编写一个函数来将Int
转换为String
?
您仍然可以通过调用java.lang.Integer
上的静态函数来使用Java转换:
val hexString = java.lang.Integer.toHexString(i)
And, starting with Kotlin 1.1, there is a function in the Kotlin standard library that does the conversion, too:
fun Int.toString(radix: Int): String
返回指定
radix
中此Int
值的字符串表示形式.
但是,请注意,Integer.toHexString()
中的this will still be different,因为后者执行无符号转换:
println((-50).toString(16)) // -32
println(Integer.toHexString(-50)) // ffffffce
But with experimental Kotlin unsigned types, it is now possible to get the same result from negative number unsigned conversion as with Integer.toHexString(-50)
:
println((-50).toUInt().toString(16)) // ffffffce