Reputation: 3503
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?
Upvotes: 26
Views: 20290
Reputation: 5645
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)
}
Upvotes: 1