I am running into some issues I didn't expect port...
# multiplatform
s
I am running into some issues I didn't expect porting some Kotlin code that I wrote into Kotlin Multiplatform. It works in a standalone Kotlin app, but not in Kotlin Multiplatform, and I'm assuming they have to be written differently in common.kt? Any suggestions on how to fix these common uses:
Copy code
fun convertHexToBitString(dataString: String) : String {
            val binaryArray: MutableList<String> = arrayListOf()
            dataString.toCharArray().forEach {
                val binValue =
                    if (it.isDigit()) {
                        String.format("%4s",
                                       Integer.toBinaryString(Character.getNumericValue(it)))
                                       .replace(' ', '0')
                    } else {
                        when(it.toUpperCase()) {
                            'A' -> "1010"
                            'B' -> "1011"
                            'C' -> "1100"
                            'D' -> "1101"
                            'E' -> "1110"
                            'F' -> "1111"
                            else -> "Invalid hexadecimal digit in string."
                        }
                    }
                binaryArray.add(binValue)
            }
            return binaryArray.joinToString("")
it.isDigit() is not working in common.kt, nor is Character.getNumericValue, and neither is String.format(). Are any of these accessible through another means in Kotlin Multiplatform?
s
isDigit
is only available in Jvm and Native. See https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/is-digit.html. If you’re not targeting JS you can use expect/actual functions for jvm and native. If you need js support you’ll need to implement the function yourself on that platform.
s
String.format
is also available only on JVM.
r
Also I think
arrayListOf()
is jvm-only but you can use
mutableListOf()
instead.