Is there a way to initialize FourCharCode with Kotlin Native
I have some C code and I want to rewrite
#define test 'test'
mohamed rejeb
08/10/2023, 10:28 AM
That's how you can get the code of a C FourCharCode in Koltin
Copy code
val charList = listOf('t', 'e', 's', 't')
val code = (charList[0].toInt() shl 24) or
(charList[1].toInt() shl 16) or
(charList[2].toInt() shl 8) or
charList[3].toInt()
m
Miroslav Sobotka
08/10/2023, 2:31 PM
Copy code
fun String.asFourCharCode(): UInt {
require(length == 4) { "Code length must be 4 characters" }
var hash: UInt = 0u
for (char in this) {
hash = (hash shl 8) or char.code.toUInt()
}
return hash
}