With Java primitives it was easy to cast char code to symbol
int i = 65;
char c = (char) i; // 'A'
How to do the same with Kotlin?
Use the int.toChar()
function to do it.
128104.toChar()
doesn't give the desired result (👨)
Dec 20, 2017 at 14:37
Returns the Char with the numeric value equal to this number, truncated to 16 bits if appropriate.
kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/to-char.html
Mar 14, 2019 at 13:47
First convert the Int to ByteArray (with the correct byte order) using a ByteBuffer, then use the appropriate String constructor.
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.charset.Charset
fun intToByteArray(n: Int, byteOrder: ByteOrder) =
ByteBuffer.allocate(4).order(byteOrder).putInt(n).array()
fun byteArrayToUnicode(ba: ByteArray, charSet: Charset) =
String(ba, charSet)
fun intToUniCode(n: Int, byteOrder: ByteOrder, charSet: Charset) =
byteArrayToUnicode(intToByteArray(n, byteOrder), charSet)
fun test() {
val charSet = Charset.forName("UTF-32BE")
val n = 0x000000f7 // division sign (U+00F7)
val s = intToUniCode(n, ByteOrder.BIG_ENDIAN, charSet)
println(s)
}